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

C/C++获取系统时间

2014-11-06 11:57 183 查看
C/C++获取系统时间需要使用Windows API,包含头文件"windows.h"。

系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义:

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;


其中包含了年、月、日、时、分、秒、毫秒、一周中的第几天(从周一开始计算)等8条信息,均为WORD类型。

在VC++6.0中WORD为无符号短整型,定义如下:

typedef unsigned short      WORD;


获取时间的函数有两个:GetLocalTime()获取本地时间;GEtSystemTime()获取格林尼治标准时间。

#include <iostream.h>
#include "windows.h"
int main(){
SYSTEMTIME ct;
GetLocalTime(&ct);//local time
//GetSystemTime(&ct);//GMT time
cout<<ct.wYear<<endl;
cout<<ct.wMonth<<endl;
cout<<ct.wDay<<endl;
cout<<ct.wHour<<endl;
cout<<ct.wMinute<<endl;
cout<<ct.wSecond<<endl;
cout<<ct.wMilliseconds<<endl;
cout<<ct.wDayOfWeek<<endl;//day of a week,start from Monday
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: