linux c++获取年月日

#include <stdio.h>
#include <string.h> 
#include <string>
#include <iostream>
using namespace std;
#define CHAR char
int GetDate(const string &fmt, string &result)
{
	CHAR buf[256];
	time_t 	currtime;
	time(&currtime);
	struct tm *today = localtime(&currtime);
	if(strftime(buf, 256, fmt.c_str(), today)>0)
	{
		result = string(buf);
		return result.size();
	}
	return -1;
}

 
int main()
{
   string Val;
   GetDate("%Y-%m-%d",Val);;
   cout<<Val<<endl;
   return 0;
}

 

posted on 2021-02-13 08:15  lydstory  阅读(356)  评论(0)    收藏  举报

导航