C/C++程序中需要程序显示当前时间,可以使用标准函数strftime。
函数原型:size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
代码示例:
1 #include <stdio.h> 2 #include <time.h> 3 4 int main () 5 { 6 time_t rawtime; 7 struct tm * timeinfo; 8 char buffer [128]; 9 10 time (&rawtime); 11 printf("%ld\n", rawtime); 12 13 timeinfo = localtime (&rawtime); 14 strftime (buffer,sizeof(buffer),"Now is %Y/%m/%d %H:%M:%S",timeinfo); 15 printf("%s\n", buffer); 16 17 return 0; 18 }
代码输出:
格式化时间说明表:
更多资源见如下链接:
cplusplus strftime:http://www.cplusplus.com/reference/ctime/strftime/?kw=strftime
http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html