VC控件DateTimePicker使用方法及其相关
獲取當(dāng)前的日期、時(shí)間并格式化輸出
框1,框2,框3:Static Text控件,修改ID
m_strDate:關(guān)聯(lián)框1,CString數(shù)據(jù)類型(Value類別)
m_strTime:關(guān)聯(lián)框2,CString數(shù)據(jù)類型(Value類別)
m_strDateTime:關(guān)聯(lián)框3,CString數(shù)據(jù)類型(Value類別)
獲取按鈕消息響應(yīng)函數(shù):
void CTest17GetTimeDlg::OnGetTime()
{
// TODO: 在此添加控件通知處理程序代碼
//UpdateData(true);
CTime m_time;
m_time=CTime::GetCurrentTime(); //獲取當(dāng)前時(shí)間日期
m_strDate=m_time.Format(_T("%x")); //格式化日期
m_strTime=m_time.Format(_T("%X")); //格式化時(shí)間
m_strDateTime=m_time.Format(_T("%Y-%m-%d %H:%M:%S %A")); //格式化日期時(shí)間
UpdateData(false);
}
注:
CTime類表示的是絕對(duì)時(shí)間,即基于格林威治平均時(shí)間(GMT);CTimeSpan則表示的是時(shí)間間隔。
GetCurrentTime函數(shù)可以獲取當(dāng)前的日期、時(shí)間;Format函數(shù)則可以將時(shí)間、日期格式化。
Format函數(shù)的格式符號(hào)及其意義:
%a: 簡(jiǎn)寫的星期名,例如Sat代表Saturday
%A: 全寫星期(英文),如Friday
%b: 簡(jiǎn)寫的月名,例如Mar代表March
%B: 月份(全寫英文),如October
%c: 月/日/年 時(shí):分:秒,如1/3/2012 21:19:11
%d: 月中的天數(shù),值為01~31
%H: 24小時(shí)格式的小時(shí)數(shù),值為00~23
%I: 通常的12小時(shí)格式的小時(shí)數(shù),值為01~12
%j: 一年當(dāng)中的第幾天,值為001~336
%m: 月數(shù),值為01~12
%M: 分鐘數(shù),值為00~59
%p: 由12小時(shí)格式的時(shí)鐘指示的am/pm(上午/下午)
%S: 秒數(shù),值為00~59
%U: 年中的周數(shù),值為00~53,以周日為一周的第一天
%w: 周中的日期數(shù),值為0~6,其中0為周日
%W: 年中的周數(shù),值為00~53,以周一為一周的第一天
%x: 月/日/年,%c的前半段
%X: 時(shí)/分/秒,%c的后半段
%y: 不帶年代前綴的年數(shù),值為00~99
%Y: 年份(帶世紀(jì),如2006)
%z: 簡(jiǎn)寫的時(shí)區(qū)名稱
%Z: 時(shí)區(qū)名稱的全稱,如果時(shí)區(qū)未知,此字符為空,如“中國(guó)標(biāo)準(zhǔn)時(shí)間”
==================================================================
來源:http://www.cnblogs.com/52yixin/articles/2111299.html
使用DateTimePicker控件一般是獲 取其時(shí)間替代手工輸入帶來的不便,而DateTimePicker控件既可以獲取日期(2010-03-05)也可以獲取時(shí)間(16:27:33),要獲 取日期只需要更改控件屬性的格式為長(zhǎng)日期或短日期,要獲取時(shí)間則將格式更改為時(shí)間即可!
???????? 在添加控件變量時(shí),選擇其變量類型為CDateTimeCtrl(類如m_DateCtrl);在程序中定義CTime對(duì)象用來保存獲取的時(shí)間,然后可以將其轉(zhuǎn)換為CString類型;具體實(shí)現(xiàn):
[cpp] view plain copy print?VC2005中DateTimePicker控件的使用
[cpp] view plain copy print?
4 CString轉(zhuǎn)COleDateTime(因?yàn)閂S2005時(shí)間日期控件關(guān)聯(lián)的Value變量默認(rèn)是COleDateTime類型)
?
[cpp] view plain copy print?SetTime時(shí) 參數(shù)需要為指針
[cpp] view plain copy print?
// 設(shè)定時(shí)間日期控件允許選擇的范圍
===================================CString 與 CTime 的相互轉(zhuǎn)化=============================================
C++中,CTime 與 CString轉(zhuǎn)換<轉(zhuǎn)>
CTime m_StartTime1 = CTime::GetCurrentTime();
CString csStartTime = m_StartTime1.Format( "%Y%m%d%H%M%S" );
使用sprintf及sscanf函數(shù)進(jìn)行CString與CTime之間的轉(zhuǎn)換
一.將CString轉(zhuǎn)為CTime的幾種方法
CString?? timestr?? =?? "2000年04月05日";??
? int?? a,b,c?? ;??
? sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c);??
? CTime?? time(a,b,c,0,0,0);????
--------or - ---------------------
CString?? s("2001-8-29?? 19:06:23");??
? int?? nYear,?? nMonth,?? nDate,?? nHour,?? nMin,?? nSec;??
? sscanf(s,?? "%d-%d-%d?? %d:%d:%d",?? &nYear,?? &nMonth,?? &nDate,?? &nHour,?? &nMin,?? &nSec);??
? CTime?? t(nYear,?? nMonth,?? nDate,?? nHour,?? nMin,?? nSec);
---- or ------------------------
CString?? timestr?? =?? "2000年04月05日";??
? int?? year,month,day;??
? BYTE?? tt[5];??
? //get?? year??
? memset(tt,?? 0,?? sizeof(tt));??
? tt[0]?? =?? timestr[0];??
? tt[1]?? =?? timestr[1];??
? tt[2]?? =?? timestr[2];??
? tt[3]?? =?? timestr[3];??
? year=?? atoi((char?? *)tt);??
???
? //get?? month??
? memset(tt,?? 0,?? sizeof(tt));??
? tt[0]?? =?? timestr[6];??
? tt[1]?? =?? timestr[7];??
? month?? =?? atoi((char?? *)tt);??
???
? //get?? day??
? memset(tt,?? 0,?? sizeof(tt));??
? tt[0]?? =?? timestr[10];??
? tt[1]?? =?? timestr[11];??
???
? CTime?? time(year,month,day,0,0,0);
從上面來看,很明顯使用sscanf()函數(shù)的優(yōu)勢(shì).
二.將CTIme轉(zhuǎn)換為CString的方法:
CTime? tmSCan = CTime::GetCurrentTime();
CString szTime = tmScan.Format("'%Y-%m-%d %H:%M:%S'");
這樣得到的日期時(shí)間字符串就是以"2006-11-27 23:30:59"的格式.這是不是很方便呢?
//取得CTime中的日期
CString cstrDate = tmScan.Format("%Y-%m-%d");
//取得CTime中的時(shí)間
CString cstrTime = tmScan.Format("%H:%M-%S");
????????? sprintf還有個(gè)不錯(cuò)的表妹:strftime,專門用于格式化時(shí)間字符串的,用法跟她表哥很像,也是一大堆格式控制符,只是畢竟小姑娘家心細(xì),她還要調(diào)用者指定緩沖區(qū)的最大長(zhǎng)度,可能是為了在出現(xiàn)問題時(shí)可以推卸責(zé)任吧。這里舉個(gè)例子:
更多更好的sprintf()函數(shù)說明參考:《spirntf,你知道多少?》
http://blog.csdn.net/steedhorse/archive/2005/03/25/330206.aspx
time_t t = time(0);
?????//產(chǎn)生"YYYY-MM-DD hh:mm:ss"格式的字符串。
char s[32];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t));
sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,這一對(duì)由于從面向?qū)ο竽睦锏玫搅速澲?#xff0c;用以寫出的代碼更覺優(yōu)雅
?
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的VC控件DateTimePicker使用方法及其相关的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 列连接_mysql – 将
- 下一篇: python 3.9.0a0_Pytho