MFC的COleDateTime類使用

2021-06-22 01:08:21 字數 2486 閱讀 2433

mfc的coledatetime類使用

date資料型別,說白了,其實就是乙個浮點值(在mfc中是double),用來度量(或者說表示吧)自2023年12月30日午夜12點以後的時間。據此:

2023年12月31日午夜12點就可以用1.0來表示(是不是過了午夜12點,就1.0+1.0呢?應該是的),以此類推

2023年1月1日上午6點就表示為2.25;同理,

2023年12月29日午夜12點標示為-1.0;

2023年12月29日上午6點標示為-1.25。

注意,如果要解析時間(time)部分就使用上述數字(如2.25)小數部分的絕對值。

coledatetime類能夠處理的日期、時間範圍為:100年1月1日---2023年12月31日,基本也夠用啦,是吧?哈哈。

注意,coledatetime不能處理夏令時。

coledatetime也可用於僅表示日期或者僅表示時間。按照慣例,如果coledatetime僅含時間值,那麼此時日期就為日期0(2023年12月30日),有點繞吧!同理,coledatetime僅表示日期時,那麼其時間就為0:00。

下面我們來看看異常的情況,該類是如何處理的。如果你建立了乙個coledatetime物件,表示的日期<100,那麼該日期會被coledatetime物件接受,但是接下來對getyear, getmonth, getday, gethour, getminute, getsecond的呼叫會失敗,返回-1(這麼看來,還不如不接受呢!)。早於mfc4.2版本時,是可以使用2位數的日期值的,但是在mfc4.2版本後,這就不允許了!為了避免上述問題,我們應該為coledatetime物件指定4位數的日期,例如:

coledatetime mytime(1996,1,1,0,0,0); // 1996--四位數

coledatetime的基礎的算數運算(+,-)結果可以使用coledatetimespan類表示(兩個coledatetime相減,等於乙個coledatetimespan值)。coledatetimespan代表相對值(還記得前文說的絕對時間值嗎?),是乙個時間間隔。上述兩個類的關係,類似於ctime與ctimespan的關係。大家可以參照。

下面上點**,看看coledatetime如何使用吧!

看看執行結果:

十秒鐘!當然這個時間不是非常的精確,不能用於火星著陸計時哦!!!

非得先用看起來很麻煩的getdays, gethours, getmin...這些函式獲取天、小時、分秒,然後再格式化字串嗎?沒有一步到位的方法嗎?當然不是啦!往下看:

coledatetimespan有個format方法,支援如下格式化字串:

%aabbreviated weekday name

%afull weekday name

%babbreviated month name

%bfull month name

%c%d

day of month as decimal number (01 – 31)

%hhour in 24-hour format (00 – 23)

%ihour in 12-hour format (01 – 12)

%jday of year as decimal number (001 – 366)

%mmonth as decimal number (01 – 12)

%mminute as decimal number (00 – 59)

%pcurrent locale』s a.m./p.m. indicator for 12-hour clock

%ssecond as decimal number (00 – 59)

%uweek of year as decimal number, with sunday as first day of week (00 – 53)

%wweekday as decimal number (0 – 6; sunday is 0)

%wweek of year as decimal number, with monday as first day of week (00 – 53)

%xdate representation for current locale

%xtime representation for current locale

%yyear without century, as decimal number (00 – 99)

%yyear with century, as decimal number

%z, %z

time-zone name or abbreviation; no characters if time zone is unknown

%%percent sign

哈哈,夠用了吧。

MFC學習 MFC入門

mfc 原作者姓名 zhoujiamurong include afxwin.h class sample public cframewnd class public public0,initinstance中呼叫 initinstance中呼叫 mb ok sample s new sample ...

MFC 關於MFC中CImage的簡單使用

首先要將 include 加進來,開始時我加到stdafx.h中,但一直提示windows.h被重複引入的問題,後將其加在別的標頭檔案中,就可以了.一 的載入 hresult load lpctstr pszfilename throw hresult load istream pstream th...

MFC學習 MFC入門 二

mfc 訊息 對映 原作者姓名 zhojiamurong include afxwin.h 定義sample繼承mfc中的cframewnd類,該類代表了窗體框架 class sample public cframewnd 新增訊息處理函式 左鍵按下事件的方法 void onlbuttondown ...