c Methods to obtain system time: 1. Use system functions, and the system time can be modified; 2. Get system time, the code is [time_t now_time=time(NULL)]; 3. Use windows API, accurate to the millisecond level.
c Method to obtain system time:
1. Use system functions and modify the system Time
#include <stdlib.h> using namespace std; void main() { system("time"); }
Note: The information obtained is hours: minutes: seconds Information
2. Get the system time (second level), It can be converted into year, month, day, week, hour, minute and second
#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;//[0~59] deadline_tm.tm_min=10;//[0~59] deadline_tm.tm_hour=13;//[0~23] deadline_tm.tm_isdst=0;//default deadline_tm.tm_mday=31;//[1~31] deadline_tm.tm_mon=2;//[0~11] }
3. Use windows API, accurate to millisecond level
#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);
##Related learning recommendations:
The above is the detailed content of How to get system time in c++?. For more information, please follow other related articles on the PHP Chinese website!