VC++ 获取系统时间的方法汇总
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm.Format("现在时间是%Y年%m月%d日 %X"); MessageBox(str,NULL,MB_OK); a,从CTimet中提取年月日时分秒 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为星期六 b,计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下: CTime t1( 1999,3,19,22,15,0 ); CTime t = CTime::GetCurrentTime(); CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔 int iDay=span.GetDays(); //获取这段时间间隔共有多少天 int iHour=span.GetTotalHours(); //获取总共有多少小时 int iMin=span.GetTotalMinutes();//获取总共有多少分钟 int iSec=span.GetTotalSeconds();//获取总共有多少秒 c,获得当前日期和时间,并可以转化为CString CTime tm=CTime::GetCurrentTime(); CString str=tm.Format("%Y-%m-%d");//显示年月日 2.使用GetLocalTime:Windows API 函数,获取当地的当前系统日期和时间 (精确到毫秒) typedef struct _SYSTEMTIME { WORD wYear;//年 WORD wMonth;//月 WORD wDayOfWeek;//星期:0为星期日,1为星期一,2为星期二…… WORD wDay;//日 WORD wHour;//时 WORD wMinute;//分 WORD wSecond;//秒 WORD wMilliseconds;//毫秒 }SYSTEMTIME,*PSYSTEMTIME; 例: 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) ; AfxMessageBox(strDate); AfxMessageBox(strTime); 3.使用GetTickCount:从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。(精确到毫秒) //获取程序运行时间 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); 4.使用time_t time( time_t * timer ) : 仅使用C标准库(精确到秒) %a 星期几的缩写。Eg:Tue 以上所述就是本文的全部内容了,希望大家能够喜欢。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |