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:
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 Number | Functions & Description |
|---|---|
| 1 | time_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. |
| 2 | char *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:
Example return value: "Tue Jul 29 19:12:15 2025\n\0" |
| 3 | struct tm *localtime(const time_t *time); This function returns a pointer to a structure representing the local time.tmPointer to the structure. |
| 4 | clock_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. |
| 5 | char * 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. |
| 6 | struct 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). |
| 7 | time_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. |
| 8 | double difftime ( time_t time2, time_t time1 ); This function returns the difference in seconds between time1 and time2. |
| 9 | size_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
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
When the above code is compiled and executed, it produces the following results:
1970 到目前时间:1503564157 年: 2017 月: 8 日: 24 时间: 16:42:37other extensions