引用 C C 獲取當前系統時間

2021-09-02 05:59:52 字數 1590 閱讀 2938

01 //方案— 優點:僅使用c標準庫;缺點:只能精確到秒級  

02 #include

03 #include

04 int main( void )   

05

顯示**列印1 size_t strftime(char *strdest, size_t maxsize, const char *format, const struct tm *timeptr);

根據格式字串生成字串。

顯示**列印1 struct tm *localtime(const time_t *timer);

取得當地時間,localtime獲取的結果由結構tm返回

返回的字串可以依下列的格式而定:

%a 星期幾的縮寫。eg:tue 

%a 星期幾的全名。 eg: tuesday

%b 月份名稱的縮寫。 

%b 月份名稱的全名。 

%c 本地端日期時間較佳表示字串。 

%d 用數字表示本月的第幾天 (範圍為 00 至 31)。日期

%h 用 24 小時制數字表示小時數 (範圍為 00 至 23)。 

%i 用 12 小時制數字表示小時數 (範圍為 01 至 12)。 

%j 以數字表示當年度的第幾天 (範圍為 001 至 366)。 

%m 月份的數字 (範圍由 1 至 12)。

%m 分鐘。 

%p 以 ''am'' 或 ''pm'' 表示本地端時間。 

%s 秒數。 

%u 數字表示為本年度的第幾周,第乙個星期由第乙個週日開始。 

%w 數字表示為本年度的第幾周,第乙個星期由第乙個周一開始。 

%w 用數字表示本週的第幾天 ( 0 為週日)。 

%x 不含時間的日期表示法。 

%x 不含日期的時間表示法。 eg: 15:26:30

%y 二位數字表示年份 (範圍由 00 至 99)。 

%y 完整的年份數字表示,即四位數。 eg:2008

%z(%z) 時區或名稱縮寫。eg:中國標準時間 

%% % 字元。

顯示**列印01 //方案二 優點:能精確到毫秒級;缺點:使用了windows api   

02 #include

03 #include

04 int main( void )   

05

顯示**列印1 //方案三,優點:利用系統函式,還能修改系統時間  

2 //此檔案必須是c++檔案  

3 #include

4 #include

5 using namespace std;  

6 void main()  

7 顯示**列印01 //方案四,將當前時間折算為秒級,再通過相應的時間換算即可  

02 //此檔案必須是c++檔案  

03 #include

04 #include

05 using namespace std;  

06 int main()  

07

文章出處:飛諾網(www.firnow.com):

C C 獲取當前系統時間

方案 優點 僅使用c標準庫 缺點 只能精確到秒級 include include int main void size t strftime char strdest,size t maxsize,const char format,const struct tm timeptr 根據格式字串生成字...

C C 獲取當前系統時間

方案三,優點 利用系統函式,還能修改系統時間 此檔案必須是c 檔案 include include using namespace std void main 方案四,將當前時間折算為秒級,再通過相應的時間換算即可 此檔案必須是c 檔案 include include using namespace...

C C 獲取當前系統時間

方案 優點 僅使用c標準庫 缺點 只能精確到秒級 include include int main void size t strftime char strdest,size t maxsize,const char format,const struct tm timeptr 根據格式字串生成字...