MFC MFC如何獲得系統時間?

2021-07-10 23:40:55 字數 821 閱讀 1816

1.使用ctime類

#include "afx.h"

void main()

解析:ctime,cstring共同用的標頭檔案為「afx.h」,

ctime類可以提取系統的當前時間函式

getcurrenttime()

,並且可以通過format方法轉換成cstring,如下例

ctime tmscan = ctime::getcurrenttime();   

cstring sztime = tmscan.format("'%y-%m-%d %h:%m:%s'");

2、得到系統時間日期(使用getlocaltime)

#include "afx.h"

void main()

解析:利用

getlocaltime

函式,獲取系統當前時間,並將獲得的值放在systemtime結構中,

同樣的也可以使用format方法,不過這裡使用的是cstring類的format方法

systemtime 

st;getlocaltime(&st);

cstring 

time;

time.format( "m-d-d 

d:d:d ", 

st.wyear.....);

3、使用gettickcount//獲取系統執行時間,也可以實現對程式的執行時間的計算

#include "afx.h"

#include "afxwin.h"

void main()

解析:gettickcount函式可以獲取系統執行的時間,利用其差值可以獲取程式的執行時間

如何在C 獲得系統時間

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

獲得系統當前時間

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

獲得系統當前時間

nsdateformatter nsdformatter nsdateformatter alloc init autorelease nsdformatter setdatestyle nsdateformattershortstyle nsdformatter setdateformat yyy...