您的位置:首页 > 编程语言 > C语言/C++

C++获取windows系统时间的七种方式

2017-06-21 10:05 274 查看
http://blog.csdn.net/bao_bei/article/details/506175052016

1.CTime类:获取系统当前时间,精确到秒

[cpp] view
plain copy

 





CString str;

CTime tm;

tm=CTime::GetCurrentTime();//获取系统日期

str=tm.Format("现在时间是%Y年%m月%d日
%X");

MessageBox(str,NULL,MB_OK);</span>



a,从CTimet中提取年月日时分秒

[cpp] view
plain copy

 





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为星期六</span>



b,计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

[cpp] view
plain copy

 





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

[cpp] view
plain copy

 





CTime tm=CTime::GetCurrentTime();

CString str=tm.Format("%Y-%m-%d");//显示年月日</span>



2. time_t,time():C标准库(精确到秒)





[cpp] view
plain copy

 





time_t time(time_t *timer);//得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数

double difftime(time_t timer1,time_t timer0);//计算时间差</span>

返回的字符串可以依下列的格式而定:

[cpp] view
plain copy

 





%a 星期几的缩写。Eg:Tue

%A 星期几的全名。 Eg: Tuesday

%b 月份名称的缩写。

%B 月份名称的全名。

%c 本地端日期时间较佳表示字符串。

%d 用数字表示本月的第几天 (范围为 00 至 31)。日期

%H 用 24 小时制数字表示小时数 (范围为 00 至 23)。

%I 用 12 小时制数字表示小时数 (范围为 01 至 12)。

%j 以数字表示当年度的第几天 (范围为 001 至 366)。

%m 月份的数字 (范围由 1 至 12)。

%M 分钟。

%p 以 ''AM'' 或 ''PM'' 表示本地端时间。

%S 秒数。

%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。

%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。

%w 用数字表示本周的第几天 ( 0 为周日)。

%x 不含时间的日期表示法。

%X 不含日期的时间表示法。 Eg: 15:26:30

%y 二位数字表示年份 (范围由 00 至 99)。

%Y 完整的年份数字表示,即四位数。 Eg:2008

%Z(%z) 时区或名称缩写。Eg:中国标准时间

%% % 字符

更细致可参考:http://blog.csdn.net/love_gaohz/article/details/6637625

3.clock_t,clock():C标准库(精确到毫秒)

其实返回的是程序运行到现在的时间

[cpp] view
plain copy

 





#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main(void)

{

long i
= 10000000L;

clock_t start,
finish;

double duration;

/* 测量一个事件持续的时间*/

printf( "Time to do %ld empty loops
is ", i) ;

start = clock();

while(
i-- );

finish = clock();

duration = (double)(finish
- start) / CLOCKS_PER_SEC; /*CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元*/

printf( "%f seconds\n",
duration );

system("pause");

}<span style="font-family: 宋体;">
</span>





4.GetTickCount()

从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。(精确到毫秒)

[cpp] view
plain copy

 





//获取程序运行时间

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);

5、QueryPerformanceFrequencyQueryPerformanceCounter:毫秒级

该方法是一种重量级的计算,可以计算非常准确的时间。一般情况下,到毫秒级别的,只需要使用clock即可。

[cpp] view
plain copy

 





LARGE_INTEGER frequency;

LARGE_INTEGER timeStart, timeEnd;

QueryPerformanceFrequency(&frequency); //
返回每秒,cpu 所使用的时钟数

QueryPerformanceCounter(&timeStart);

Sleep(100);

QueryPerformanceCounter(&timeEnd);

std::cout << difftime(timeEnd.QuadPart, timeStart.QuadPart)* 1000/frequency.QuadPart << std::endl;

6、timeGetTime()

也是精确到毫秒级别的时间函数,但是该函数只是一个轻量级的时间函数

例:

[cpp] view
plain copy

 





//需要 #pragma comment(lib, "winmm.lib")

DWORD dw
= timeGetTime();

std::cout << dw << std::endl;

Sleep(120);

DWORD dwEnd
= timeGetTime();

std::cout << difftime(dwEnd, dw) << std::endl; //
我的电脑显示的是121

7、GetLocalTime、GetSystemTime:Windows API 函数

GetLocalTime,获取当地的当前系统日期和时间 (精确到毫秒)

GetSystemTime,则获取的是标准的时间。

两个函数会把获取的系统时间信息存储到SYSTEMTIME结构体里边

[cpp] view
plain copy

 





typedef struct _SYSTEMTIME

{

  WORD wYear;//年

  WORD wMonth;//月

  WORD wDayOfWeek;//星期:0为星期日,1为星期一,2为星期二……

  WORD wDay;//日

  WORD wHour;//时

  WORD wMinute;//分

  WORD wSecond;//秒

  WORD wMilliseconds;//毫秒

}SYSTEMTIME,*PSYSTEMTIME;

例:

[cpp] view
plain copy

 





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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows c语言 cpp