Get current time and date in C++
This post will discuss how to get the current time and date in C++.
1. Using std::chrono
Since C++11, the standard solution to get the current time and date in C++ is using chrono library. We can get the current time with std::chrono::system_clock::now() from the <chrono.h> header, and convert it to a std::time_t type (time since epoch). Then, convert the std::time_t to a local calendar time std::ctime in Www Mmm dd hh:mm:ss yyyy format, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <chrono> int main() { auto now = std::chrono::system_clock::now(); std::time_t end_time = std::chrono::system_clock::to_time_t(now); std::cout << "Current Time and Date: " << std::ctime(&end_time) << std::endl; return 0; } |
Output (will vary):
Current Time and Date: Tue Feb 08 18:40:13 2018
If you just need to measure elapsed time in C++, do as follows.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <chrono> int main() { auto start = std::chrono::system_clock::now(); // do some work for (int i = 0; i < 10000000; i++) {} auto end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; std::cout << "Elapsed Time: " << elapsed_seconds.count() << " sec" << std::endl; return 0; } |
Output (will vary):
Elapsed Time: 0.060156 sec
Since C++20, we can also use std::chrono::zoned_time for representing date and time with a time-zone.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <chrono> int main() { std::chrono::zoned_time now { std::chrono::current_zone(), std::chrono::system_clock::now() }; std::cout << "Current Time and Date: " << std::endl; return 0; } |
2. Using std::time
The idea is to get the number of seconds elapsed since epoch as std::time_t value, and then convert it into an std::tm instance with std::localtime. An std::tm object holds a calendar date and time broken down into its components (sec, min, hour, day, month, year, etc.).
The following code example demonstrates its usage. Note that this solution needs <ctime> header.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <ctime> int main() { std::time_t t = std::time(nullptr); std::tm* now = std::localtime(&t); std::cout << "Current Date: " << now->tm_mday << '/' << (now->tm_mon + 1) << '/' << (now->tm_year + 1900) << std::endl; return 0; } |
Output (will vary):
Current Date: 8/2/2018
We can further convert date and time information from the std::tm object to a null-terminated multibyte character, according to the specified format string using the strftime() function. For example, the following code returns the current date and time as std::string in the format MM-DD-YYYY HH:mm:ss.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <ctime> std::string currentDateTime() { std::time_t t = std::time(nullptr); std::tm* now = std::localtime(&t); char buffer[128]; strftime(buffer, sizeof(buffer), "%m-%d-%Y %X", now); return buffer; } int main() { std::cout << "Current Time and Date: " << currentDateTime() << std::endl; return 0; } |
Output (will vary):
Current Time and Date: 02-08-2018 18:42:41
3. Using Boost.Date_Time
Another option is to use the date-time functions offered by the boost library. There are several time systems available by boost library. The following solution uses boost::posix_time::microsec_clock::universal_time from the header <boost/date_time/posix_time/posix_time> – that return the time in UTC.
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::posix_time::ptime datetime = boost::posix_time::microsec_clock::universal_time(); std::cout << "Current Time and Date: " << datetime << std::endl; return 0; } |
Output (will vary):
Current Time and Date: 2018-Feb-08 18:42:41.804879
That’s all about getting the current time and date in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)