C++ Date Time

C++ inherits data types for time from C language. To use these data types in your program you have to include ctime header:

#include <ctime>

This header provides 4 data types used for time representation:

  • clock_t – Clock type
  • size_t – Unsigned integral type
  • time_t – Time type
  • struct tm – Time structure

The first 3 data types represent time as integers and you will need to convert these integers to get commonly used representation of time.
The most user friendly way of time representation has struct tm. What is a structure is discussed in the C++ Data Structures . The tm has the following fields that represent time:

 Field Type  Meaning  Range
 tm_sec int seconds 0-61
 tm_min int minutes 0-59
 tm_hour int hours 0-23
 tm_mday int day of the month 1-31
 tm_mon int months since January 0-11
 tm_year int years since 1900
 tm_wday int days since Sunday 0-6
 tm_yday int days since January 1 0-365


To use a variable of type tm you can declare it in the same way you declare any variable:

tm my_time;

The ctime header provides a range of useful functions to work with data types:

  • char* asctime (const struct tm * timeptr); converts pointer to struct tm to an array of chars
  • char* ctime (const time_t * timer); converts value of a time_t value to a char array in format Www Mmm dd hh:mm:ss yyyy (Www – weekday, Mmm – month, dd – day of the week, dd – date, mm – minutes, ss- seconds, hh – hours, yyyy – year).
  • struct tm * gmtime (const time_t * timer); convert a time_t value to struct tm  as UTC time.
  • struct tm * localtime (const time_t * timer); convert a time_t value to struct tm  in local time format.
  • size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );  this functions copies the time value of timeptr according to the format into an array of char ptr of maximum size maxsize.

The main format specifiers for this function are:

 Specifier Meaning
 %a Abbreviated weekday name
 %A Full weekday name
 %b Abbreviated month name
 %B Full month name
 %c Date and time representation
 %C Year divided by 100 and truncated to integer (00-99)
 %d Day of the month with 2 digits (01-31)
 %D Short MM/DD/YY date, equivalent to %m/%d/%y
 %e Day of the month space-padded ( 1-31)
 %F Short YYYY-MM-DD date, equivalent to %Y-%m-%d
 %g Week-based year, last two digits (00-99)
 %G Week-based year
 %h Abbreviated month name  (same as %b)
 %H Hour in 24h format (00-23)
 %I Hour in 12h format (01-12)
 %j Day of the year (001-366)
 %m Month as a decimal number (01-12)
 %M Minute (00-59)
 %p AM or PM designation
 %R 24-hour HH:MM time, equivalent to %H:%M
 %S Second (00-61)


  • clock_t clock (void); – returns the time consumed by the program from its launch. The return value is number of clock ticks. You can convert this value to seconds using CLOCKS_PER_SEC constant.
  • time_t mktime (struct tm * timeptr); – coverts tm structure  to time_t.
  • time_t time (time_t* timer); – gets the current time in format of  time_t by using a timer. You can use NULL as the parameter for this function: time(NULL)

Using these functions with modern compilers can lead to an error message:

error C4996: ‘ctime’: This function or variable may be unsafe. Consider using ctime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. ”

If you are sure that your program is safe, you can disable this error by using the following directive:

#pragma warning(disable : 4996)

This is a simple demo program that shows how you can work with time using described functions:

//get the starting value of clock
clock_t start = clock();
tm* my_time;

//get current time in format of time_t
time_t t = time(NULL);

//show the value stored in t
cout << "Value of t " << t << endl;

//convert time_t to char*
char* charTime = ctime(&t);

//display current time
cout << "Now is " << charTime << endl;

//convert time_t to tm
my_time = localtime(&t);

//get only hours and minutes
char* hhMM = new char[6];
strftime(hhMM, 6, "HH:MM", my_time);

//show a part of tm struct
//the operator -> is used to access members of the tm struct. It's described in the data structures topic
cout << "Year " << 1900 + my_time->tm_year << endl;
cout << "Month " << my_time->tm_mon << endl;
clock_t end = clock();
clock_t exec = end - start;
cout << "Program is executed in " << exec << " clocks or "
<< 1000 * exec / CLOCKS_PER_SEC << " milliseconds" << endl;

The output for this program is:

Value of t 1417965525
Now is Sun Dec 07 17:18:45 2014
Year 2014
Month 11
Program is executed in 6 clocks or 6 milliseconds

Translate »