int main()
{
time_t rawtime;
tm ptminfo;
time(&rawtime);
localtime_s(&ptminfo,&rawtime);
char buffer[256];
sprintf_s(buffer,"current: %02d%02d%02d%02d%02d%02d\n",
ptminfo.tm_year + 1900, ptminfo.tm_mon + 1, ptminfo.tm_mday,
ptminfo.tm_hour, ptminfo.tm_min, ptminfo.tm_sec);
string str = buffer;
cout << str << endl;
return 0;
}
c++随手笔记:获取当前系统时间
本文介绍了一个使用C++获取当前时间并将其格式化为字符串的示例代码。通过调用time()和localtime_s()函数来获取本地时间信息,并利用sprintf_s()将时间格式化为指定格式。

被折叠的 条评论
为什么被折叠?



