轉自:http://www.cnblogs.com/xudong-bupt/p/3550157.html
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 }
代碼輸出:
格式化時間說明表: