首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

系统时间显示出来了,为什么不是时间的格式?该如何处理

2012-09-18 
系统时间显示出来了,为什么不是时间的格式?C/C++ code#include iostream#include ctime#include Wind

系统时间显示出来了,为什么不是时间的格式?

C/C++ code
#include <iostream>#include <ctime>#include <Windows.h>using namespace std;int main(){    time_t now_time;    now_time = time(NULL);    cout << now_time << endl;    cout << GetCurrentTime() << endl;    system("pause");    return 0;} 

源代码如上,为什么打印出来的是这样的:
1346293134
2612704
按任意键继续……
谢谢各位!

[解决办法]
要打印出时间格式的字符串,要自己处理一下。
1 调用time获取时间
2 调用localtime 将time_t类型的时间装换为本地时间
3 再调用localtime 把本地时间转换为字符串string

Example:
/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
  
return 0;
}


Output:
 Current local time and date: Sat May 20 17:36:17 2000 
 

热点排行