获取系统时间

上传人:鲁** 文档编号:508529383 上传时间:2023-02-02 格式:DOCX 页数:7 大小:45.53KB
返回 下载 相关 举报
获取系统时间_第1页
第1页 / 共7页
获取系统时间_第2页
第2页 / 共7页
获取系统时间_第3页
第3页 / 共7页
获取系统时间_第4页
第4页 / 共7页
获取系统时间_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《获取系统时间》由会员分享,可在线阅读,更多相关《获取系统时间(7页珍藏版)》请在金锄头文库上搜索。

1、 欢迎阅读本文档,希望本文档能对您有所帮助!/获取系统日期时间CTime time=CTime:GetCurrentTime(); FileNameString = time.Format(%Y%m%d%H%M%S);CString PathString = .;FileNameString.Insert(0,PathString);PathString = .dat;PathString.Insert(0,FileNameString);FileNameString = PathString;VC+中其实还是通过调用它自带的CTime类来完成的获取当前系统时间的,我们做一个小程序来了解这个过

2、程吧!对话框里只有两个显示框和两个按钮,点下按钮显示当前时间。就这么一个小程序。(1)建立应用程序外壳 创建一个新的AppWizard项目,命名为shiyan,选择Dialog based;其他都选用默认属性,单击Finish完成生成应用程序的步骤。进入对话框界面以后,按下图所示布置显示框和功能按钮。(2)设置参数其中上面的显示文本框设为CString型,命名为m_show,ID号为IDC_show。下面的显示文本框设为CString型,命名为m_show1,ID号为IDC_show1。(3)编译程序start键程序:void CDate1Dlg:Onstart()/count=0;SetTi

3、mer(1,1000,NULL);/ TODO: Add your control notification handler code herestop键程序:void CDate1Dlg:Onstop()KillTimer(1);/ TODO: Add your control notification handler code here(4)增加计时器控件 View - ClassWizard - MessageMaps - CUse progressDlg,加入WM_TIMER函数,编辑程序:void CDate1Dlg:OnTimer(UINT nIDEvent)if(nIDEvent

4、=1)/count+;UpdateData(1);mtime=CTime:GetCurrentTime();/获取当前时间char i;CString w;i=char (mtime.GetDayOfWeek();/获取当前时间的天数是这个星期的第几天,这里要注意了,系统上默认的一个星期的第一天是星期日,最后一天是周六,大家千万不要搞错了。我也是试了才知道的。switch(i)/将数字状换成字符就不会出现星期7这种情况了 case 2: w=一;break; case 3: w=二;break; case 4: w=三;break; case 5: w=四;break; case 6: w=五

5、;break; case 7: w=六;break; case 1: w=日;break;int a,b,c,d,e,f;a=int(mtime.GetSecond()/10; /获取当前时间的秒数 b=int(mtime.GetSecond()%10;c=int(mtime.GetMinute()/10;/获取当前时间的分数d=int(mtime.GetMinute()%10;e=int(mtime.GetHour()/10;/获取当前时间的小时数f=int(mtime.GetHour()%10; m_show.Format(今天是%d年%d月%d日 星期%s ,mtime.GetYear(

6、),mtime.GetMonth(),mtime.GetDay(),w); m_show1.Format(现在时间是 %d%d:%d%d:%d%d,e,f,c,d,a,b); UpdateData(0);/ TODO: Add your message handler code here and/or call defaultCDialog:OnTimer(nIDEvent);之后编译运行一下就行了,效果见下图:一、使用MFC可以用以下代码得到:CTime time = CTime:GetCurrentTime(); /构造CTime对象int m_nYear = time.GetYear()

7、; /年int m_nMonth = time.GetMonth(); /月int m_nDay = time.GetDay(); /日int m_nHour = time.GetHour(); /小时int m_nMinute = time.GetMinute(); /分钟int m_nSecond = time.GetSecond(); /秒 / CTime time = CTime:GetCurrentTime();/CString strTime = time.Format(%Y-%m-%d %H:%M:%S); /Format(%I:%M:%S %p, %A, %B %d, %Y)我

8、们还可以用CTime:Format函数将CTime对象转换为字符串对象例如:CString m_strTime = time.Format(%Y-%m-%d %H:%M:%S);运行结果:m_strTime为 2001-8-1 12:11:05函数GetSystemTime和GetLocalTime声明如下:WINBASEAPIVOIDWINAPIGetSystemTime( _out LPSYSTEMTIME lpSystemTime );WINBASEAPIVOIDWINAPIGetLocalTime( _out LPSYSTEMTIME lpSystemTime );lpSystemTi

9、me是获取系统时间的结构。调用函数的例子如下:/获取系统时间。/蔡军生 2007/11/11 QQ:9073204 深圳void TestSystem(void) /获取系统的UTC时间。 SYSTEMTIME stUTC; :GetSystemTime(&stUTC); /显示时间的间隔。 const int nBufSize = 256; TCHAR chBufnBufSize; wsprintf(chBuf,_T(UTC: %u/%u/%u %u:%u:%u:%u %drn), stUTC.wYear, stUTC.wMonth, stUTC.wDay, stUTC.wHour, stU

10、TC.wMinute, stUTC.wSecond, stUTC.wMilliseconds,stUTC.wDayOfWeek); OutputDebugString(chBuf); /获取当地的时间。 SYSTEMTIME stLocal; :GetLocalTime(&stLocal); /显示时间的间隔。 wsprintf(chBuf,_T(Local: %u/%u/%u %u:%u:%u:%u %drn), stLocal.wYear, stLocal.wMonth, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSeco

11、nd, stLocal.wMilliseconds,stLocal.wDayOfWeek); OutputDebugString(chBuf);上面两个函数在我测试时输出的结果,如下:UTC: 2007/11/11 1:53:1:46 0Local: 2007/11/11 9:53:1:46 0strTime.Format(_T(%d-%d-%d %d:%d:%d.%d ),stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond,stLocal.wMilliseconds);

12、 /时间显示在VC+中,我们可以借助CTime时间类,获取系统当前日期: CTime t = CTime:GetCurrentTime(); /获取系统日期 int d=t.GetDay(); /获得几号 int y=t.GetYear(); /获取年份 int m=t.GetMonth(); /获取当前月份 int h=t.GetHour(); /获取当前为几时 int mm=t.GetMinute(); /获取分钟 int s=t.GetSecond(); /获取秒 int w=t.GetDayOfWeek(); /获取星期几,注意1为星期天,7为星期六 1.使用CTime类 CTime

13、tm=CTime:GetCurrentTime(); CString 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) /程序段 long t2=GetTickCount();/程序段结束后取得系统运行时间(ms) long t

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 国内外标准规范

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号