C++ Date & Time

The C++ standard library does not provide a so-called date type. C++ inherits the structures and functions from the C language for date and time operations. To use date and time related functions and structures, you need to include the <ctime> header file in a C++ program.

There are four time-related types:clock_t、time_t、size_tandtmThe types clock_t, size_t, and time_t can represent the system time and date as some kind of integer.

structure typetmThe date and time are stored in the form of a C structure. The tm structure is defined as follows:

struct tm { int tm_sec; // Seconds, normal range 0 to 59, but allowed up to 61 int tm_min; // Minutes, range 0 to 59 int tm_hour; // Hours, range 0 to 23 int tm_mday; // Day of the month, range 1 to 31 int tm_mon; // Month, range 0 to 11 int tm_year; // Years since 1900 int tm_wday; // Day of the week, range 0 to 6, starting from Sunday int tm_yday; // Day of the year, range 0 to 365, starting from January 1 int tm_isdst; // Daylight saving time };

Below are the important functions related to date and time in C/C++. All of these functions are part of the C/C++ standard library. You can look up the details of each function in the C++ standard library.

Serial NumberFunctions & Description
1time_t time(time_t *time);
This function returns the system's current calendar time, which is the number of seconds elapsed since January 1, 1970. If the system has no time, it returns -1.
2char *ctime(const time_t *time);
This function returns a pointer to a string whose content represents the local time, in the format:
Www Mmm dd hh:mm:ss yyyy\n\0

Where:

  • Www: weekday (e.g., Tue)
  • Mmm: month (e.g., Jul)
  • dd: day (e.g. 29)
  • hh:mm:ss: Time (24-hour clock)
  • yyyy: year (e.g., 2025)
  • Ends with a newline character\nand terminator\0

Example return value:

"Tue Jul 29 19:12:15 2025\n\0"
3struct tm *localtime(const time_t *time);
This function returns a pointer to a structure representing the local time.tmPointer to the structure.
4clock_t clock(void);
This function returns the processor time used since program execution began (usually the beginning of the program). If the time is unavailable, it returns -1.
5char * asctime ( const struct tm * time );
This function returns a pointer to a string. The string contains the information stored in the structure pointed to by time. The return format is: day month date hours:minutes:seconds year\n\0.
6struct tm *gmtime(const time_t *time);
This function returns a pointer to time, where time is a tm structure, expressed in Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT).
7time_t mktime(struct tm *time);
This function returns the calendar time, which is equivalent to the time stored in the structure pointed to by time.
8double difftime ( time_t time2, time_t time1 );
This function returns the difference in seconds between time1 and time2.
9size_t strftime();
This function can be used to format dates and times into a specified format.

Current date and time

The following example obtains the current system date and time, including local time and Coordinated Universal Time (UTC).

Example

#include <iostream> #include <ctime> using namespace std; int main( ) { // Current date/time based on current system time_t now = time(0); // Convert now to string form char* dt = ctime(&now); cout << "Local date and time:" << dt << endl; // Convert now to tm structure tm *gmtm = gmtime(&now); dt = asctime(gmtm); cout << "UTC date and time:"<< dt << endl; }

When the above code is compiled and executed, it produces the following results:

本地日期和时间:Sat Jan  8 20:07:41 2011

UTC 日期和时间:Sun Jan  9 03:07:41 2011

Formatting Time Using the tm Structure

tmThe structure is particularly important when handling date and time related operations in C/C++. The tm structure holds the date and time in the form of a C structure. Most time-related functions use the tm structure. The example below uses the tm structure and various date and time related functions.

Before practicing using structures, you need a basic understanding of C structures and know how to use the arrow -> operator to access structure members.

Example

#include <iostream> #include <ctime> using namespace std; int main( ) { // Current date/time based on current system time_t now = time(0); cout << "Seconds elapsed since 1970:" << now << endl; tm *ltm = localtime(&now); // Output the components of the tm structure cout << "Year:"<< 1900 + ltm->tm_year << endl; cout << "Month:"<< 1 + ltm->tm_mon<< endl; cout << "Day:"<< ltm->tm_mday << endl; cout << "Time:"<< ltm->tm_hour << ":"; cout << ltm->tm_min << ":"; cout << ltm->tm_sec << endl; }

When the above code is compiled and executed, it produces the following results:

1970 到目前时间:1503564157
年: 2017
月: 8
日: 24
时间: 16:42:37
other extensions