C++ 日期 和时间
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
time_t now = time(0);// 基于当前系统的当前日期/时间
char* dt = ctime(&now);// 把 now 转换为字符串形式
cout << "本地日期和时间:" << dt << endl;
tm *gmtm = gmtime(&now);// 把 now 转换为 tm 结构
dt = asctime(gmtm);
cout << "UTC 日期和时间:"<< dt << endl;
}
翻译过来会显示你电脑上的时间,很有趣。
本地日期和时间:Sat Jan 8 20:07:41 2011
UTC 日期和时间:Sun Jan 9 03:07:41 2011