码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-01-09 12:43:34      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:mfc   获取系统时间   vc++   ctime   getlocaltime   

<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++编程中获取系统时间

标签:mfc   获取系统时间   vc++   ctime   getlocaltime   

原文地址:http://blog.csdn.net/u014028070/article/details/42551045

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!