c++ get current system time code example
Example 1: how to get current time in c++
#include <ctime>
time_t tt;
time( &tt );
tm TM = *localtime( &tt );
int month=TM.tm_mon+1;
int day=TM.tm_mday;
int year=TM.tm_year+1900;
Example 2: c++ print current time
time_t now = time(0);
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}