How to get current time in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

How to get current time in C++?

I think it is done by importing <ctime.h> but what is the exact procedure to display time as output?

18th Jan 2018, 7:35 PM
Infinity
Infinity - avatar
4 Answers
+ 9
An example of the use of ctime. I have used ctime in this count down https://code.sololearn.com/cdBAJw9Lhy65/?ref=app
21st Jan 2018, 11:44 PM
Thor Westergren
Thor Westergren - avatar
+ 8
Gives The GMT timing cout<<__TIME__;
22nd Jan 2018, 2:14 AM
I'm_Groot
I'm_Groot - avatar
+ 5
time(NULL) will return amount of seconds from 01.01.1970 00:00. Then you can get current date and time using mathematics, for example, (this amount)%60 is what second is now.
18th Jan 2018, 7:55 PM
Petr Leliaev
Petr Leliaev - avatar
+ 3
don't use <ctime.h> use <ctime> in C++. and you're right, you'll need to use ctime. time_t raw; time(&raw); struct tm* current = local time(&raw); string currentTime = asctime(current); obviously you can use something like boost which would be efficient as this is.
18th Jan 2018, 7:58 PM
Cain Eviatar
Cain Eviatar - avatar