C++获取年月日


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdbool.h>

#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>

#include <iostream>
using namespace std;
static const size_t MAX_TIME_STRING_LEN = 128 ; 
// get current YYYYMMDD
bool getLocalDate(string& strDate) 
{
		struct tm newTM;
		time_t tmpTimeSec ; 
		struct timeval timeVal ; 
		gettimeofday(&timeVal, NULL) ;
		tmpTimeSec = timeVal.tv_sec ; 
	
		localtime_r( &(tmpTimeSec), &newTM);
		char chTmp[10]; memset(chTmp, 0, 10);
		snprintf(chTmp, MAX_TIME_STRING_LEN, "%04d%02d%02d", (newTM.tm_year+1900), newTM.tm_mon+1, newTM.tm_mday ) ; 
		strDate = chTmp;
		return true ; 
}

int main()
{
   string str123;
  getLocalDate(str123);
   cout<<str123<<endl;
   cout<<"2021,新年快乐"<<endl;
   return 0;
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM