- 框1,框2,框3:Static Text控件,修改ID
- m_strDate:关联框1,CString数据类型(Value类别)
- m_strTime:关联框2,CString数据类型(Value类别)
- m_strDateTime:关联框3,CString数据类型(Value类别)
- 获取按钮消息响应函数:
- void CTest17GetTimeDlg::OnGetTime()
- {
- // TODO: 在此添加控件通知处理程序代码
- //UpdateData(true);
- CTime m_time;
- m_time=CTime::GetCurrentTime(); //获取当前时间日期
- m_strDate=m_time.Format(_T("%x")); //格式化日期
- m_strTime=m_time.Format(_T("%X")); //格式化时间
- m_strDateTime=m_time.Format(_T("%Y-%m-%d %H:%M:%S %A")); //格式化日期时间
- UpdateData(false);
- }
- 注:
- CTime类表示的是绝对时间,即基于格林威治平均时间(GMT);CTimeSpan则表示的是时间间隔。
- GetCurrentTime函数可以获取当前的日期、时间;Format函数则可以将时间、日期格式化。
- Format函数的格式符号及其意义:
- %a: 简写的星期名,例如Sat代表Saturday
- %A: 全写星期(英文),如Friday
- %b: 简写的月名,例如Mar代表March
- %B: 月份(全写英文),如October
- %c: 月/日/年 时:分:秒,如1/3/2012 21:19:11
- %d: 月中的天数,值为01~31
- %H: 24小时格式的小时数,值为00~23
- %I: 通常的12小时格式的小时数,值为01~12
- %j: 一年当中的第几天,值为001~336
- %m: 月数,值为01~12
- %M: 分钟数,值为00~59
- %p: 由12小时格式的时钟指示的am/pm(上午/下午)
- %S: 秒数,值为00~59
- %U: 年中的周数,值为00~53,以周日为一周的第一天
- %w: 周中的日期数,值为0~6,其中0为周日
- %W: 年中的周数,值为00~53,以周一为一周的第一天
- %x: 月/日/年,%c的前半段
- %X: 时/分/秒,%c的后半段
- %y: 不带年代前缀的年数,值为00~99
- %Y: 年份(带世纪,如2006)
- %z: 简写的时区名称
- %Z: 时区名称的全称,如果时区未知,此字符为空,如“中国标准时间”
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);//获取系统运行时间
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);