VC++编程中获取系统时间

时间:2022-05-02 12:54:27
<span style="white-space:pre">	</span>总结了在程序中如何获得系统时间的方法
void CGetSystenTimeDlg::OnBnClickedGettimeButton()
{
// TODO: 在此添加控件通知处理程序代码
//方法一 使用MFC的CTime类
CString str; //获取系统时间
CTime tm; tm=CTime::GetCurrentTime();
str=tm.Format("现在时间是%Y年%m月%d日 %X"); MessageBox(str,NULL,MB_OK); ////方法二 使用win32定义得结构体
SYSTEMTIME time;
CString str1,str2;
GetLocalTime(&time); //Windows API 函数,用来获取当地的当前系统日期和时间。
str1.Format(L"%d-%d-%d",time.wYear,time.wMonth,time.wDay);
str2.Format(L"%2d:%2d:%2d",time.wHour,time.wMinute,time.wSecond);
MessageBox(str1,NULL,MB_OK);
MessageBox(str2,NULL,MB_OK); //方法三:GetTickCount返回(retrieve)从操作系统启动所经过的毫秒数
//,它的返回值是DWORD。 可以用它来测量程序的运行时间
CString str3;
long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)   
Sleep(500);
long t2=GetTickCount();//程序段结束后取得系统运行时间(ms)   
str3.Format(L"time:%dms",t2-t1);//前后之差即 程序运行时间   
AfxMessageBox(str3);//获取系统运行时间   即休眠的的时间  //从操作系统启动所经过的时间
long t=GetTickCount();
CString str4;
CString str5;
str4.Format(L"系统已运行 %d时",t/3600000);
str5=str5+str4;
// MessageBox(str4,NULL,MB_OK);
t%=3600000; str4.Format(L"系统已经运行 %d分",t/60000);
str5=str5+str4;
t%=60000;
str4.Format(L"系统已经运行 %d秒",t/1000);
str5=str5+str4; MessageBox(str5,NULL,MB_OK); }

方法一:

VC++编程中获取系统时间

方法二:

VC++编程中获取系统时间      VC++编程中获取系统时间

方法三:

VC++编程中获取系统时间