本文摘自PHP中文网,作者coldplay.xixi,侵删。
c++获取系统时间的方法:1、使用系统函数,并且可以修改系统时间;2、获取系统时间,代码为【time_t now_time=time(NULL)】;3、使用windows API ,精确到毫秒级。

c++获取系统时间的方法:
1、使用系统函数,并且可以修改系统时间
1 2 3 4 5 6 7 8 | # include <stdlib.h>
using namespace std;
void main()
{
system( "time" );
}
|
备注:获取的为 小时:分钟:秒 信息

2、获取系统时间(秒级),可以换算为年月日星期时分秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # include <iostream>
# include <time.h>
using namespace std;
void main()
{
time_t now_time=time(NULL);
tm* t_tm = localtime(&now_time);
printf( "local time is : %s\n" , asctime(t_tm));
time_t mk_time = mktime (t_tm);
struct tm deadline_tm;
deadline_tm.tm_sec=0;
deadline_tm.tm_min=10;
deadline_tm.tm_hour=13;
deadline_tm.tm_isdst=0;
deadline_tm.tm_mday=31;
deadline_tm.tm_mon=2;
}
|

3、使用windows API ,精确到毫秒级
1 2 3 4 5 6 7 8 9 | # include <windows.h>
# include <stdio.h>
using namespace std;
void main()
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n" ,sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
|

相关学习推荐:C视频教程
以上就是c++如何获取系统时间?的详细内容!
相关阅读 >>
C++笔试题之实现简单记录错误功能
C++标识符命名规则
区分C++常量表达式、const、constexpr(附代码)
如何用C++读取ini文件中的section节名
c#调用C++ 动态链接库dll
C++ 引用和指针区别
c 语言和 C++ 有什么区别
C++如何获取系统时间?
C++中main函数的返回值类型是什么
C++ 布尔类型和引用的用法详解
更多相关阅读请进入《C++》频道 >>
转载请注明出处:木庄网络博客 » c++如何获取系统时间?