C C 獲取當前系統時間

2022-06-18 10:54:10 字數 2841 閱讀 7023

個人覺得第二種還是比較實用的,而且也是最常用的~

不過當計算演算法耗時的時候,不要忘記second,不能只

要用milliseconds來減,不然後出現負值,若是演算法耗時太長就得用minutes啦。再不然,就hours……

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

#include

#include

int main( void ) 

size_t strftime(char *strdest, size_t maxsize, const char *format, const struct tm *timeptr);

根據格式字串生成字串。

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:中國標準時間 

%% % 字元。

//方案二 優點:能精確到毫秒級;缺點:使用了windows api 

#include

#include

int main( void ) 

//方案三,優點:利用系統函式,還能修改系統時間

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

#include

#include

using namespace std;

void main()

//方案四,將當前時間折算為秒級,再通過相應的時間換算即可

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

#include

#include

using namespace std;

int main()

size_t strftime(char *strdest, size_t maxsize, const char *format, const struct tm *timeptr);

根據格式字串生成字串。

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:中國標準時間 

%% % 字元。

//方案二 優點:能精確到毫秒級;缺點:使用了windows api 

#include

#include

int main( void ) 

//方案三,優點:利用系統函式,還能修改系統時間

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

#include

#include

using namespace std;

void main()

//方案四,將當前時間折算為秒級,再通過相應的時間換算即可

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

#include

#include

using namespace std;

int main()

{time_t now_time;

now_time = time(null);

cout<

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 根據格式字串生成字...