How to get system time in c++?

coldplay.xixi
Release: 2020-07-07 14:16:20
Original
15695 people have browsed it

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.

How to get system time in c++?

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");
}
Copy after login

Note: The information obtained is hours: minutes: seconds Information

How to get system time in c++?

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]  
}
Copy after login

How to get system time in c++?

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);
Copy after login

How to get system time in c++?

##Related learning recommendations:

C video tutorial

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template