系统时间显示出来了,为什么不是时间的格式?该如何处理
系统时间显示出来了,为什么不是时间的格式?
源代码如上,为什么打印出来的是这样的:
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
- 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