C語言 strftime 格式化顯示日期時間 時間戳


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM