vc 獲取當前時間

2021-05-28 01:48:25 字數 1105 閱讀 6588

1.使用ctime類

cstring str;

//獲取系統時間

ctime tm;

tm=ctime::getcurrenttime();

str=tm.format("現在時間是%y年%m月%d日 %x");

messagebox(str,null,mb_ok);

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

systemtime st;

cstring strdate,strtime;

getlocaltime(&st);

strdate.format("%4d-%2d-%2d",st.wyear,st.wmonth,st.wday);

strtime.format("%2d:%2d:%2d",st.whour,st.wminute,st.wsecond);

3.使用gettickcount

//獲取程式執行時間

long t1=gettickcount();//程式段開始前取得系統執行時間(ms)

//sleep(500);

long t2=gettickcount();();//程式段結束後取得系統執行時間(ms)

str.format("time:%dms",t2-t1);//前後之差即 程式執行時間

afxmessagebox(str);

4.獲取系統執行時間

long t=gettickcount();

cstring str,str1;

str1.format("系統已執行 %d時",t/3600000);

str=str1;

t%=3600000;

str1.format("%d分",t/60000);

str+=str1;

t%=60000;

str1.format("%d秒",t/1000);

str+=str1;

afxmessagebox(str);

5.計算從2023年1月1日0時0分0秒到該時間點所經過的秒數

#include

#include

using namespace std;

int main()

獲取當前時間

獲取日期 時間 datetime.now.tostring 2008 9 4 20 02 10 datetime.now.tolocaltime tostring 2008 9 4 20 12 12 獲取日期 datetime.now.tolongdatestring tostring 2008年9...

獲取當前時間

直接上可以執行的 檔案 獲取當前時間.cpp 方案 time 優點 僅使用c標準庫 缺點 只能精確到秒級 include time.h include stdio.h int main void 方案二getlocaltime 優點 能精確到毫秒級 缺點 使用了windows api include...

獲取當前時間

使用函式 date 實現 顯示的格式 年 月 日 小時 分鐘 妙 相關時間引數 a am 或是 pm a am 或是 pm d 幾日,二位數字,若不足二位則前面補零 如 01 至 31 d 星期幾,三個英文本母 如 fri f 月份,英文全名 如 january h 12 小時制的小時 如 01 至...