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