Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Date & Time

C++ Date & Time

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

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

Structure type tm stores date and time 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 month, range 1 to 31

int

tm_mon

;

// month, range 0 to 11

int

tm_year

;

// years since 1900

int

tm_wday

;

// day of week, range 0 to 6, starting from Sunday

int

tm_yday

;

// day of year, range 0 to 365, starting from January 1

int

tm_isdst

;

// daylight saving time

}

;

The following are important date- and time-related functions in C/C++. All these functions are part of the C/C++ standard library; you can look up the details of each function in the C++ standard library.

No. Function & Description
1 time_t time(time_t *time);
This function returns the system's current calendar time as 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);
 返回一个指向字符串的指针,字符串内容表示本地时间,格式为:     
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 format)
  • yyyy: year (e.g., 2025)
  • A newline at the end \n and a terminator \0
  • Example return value:

    "Tue Jul 29 19:12:15 2025\n\0"
    | | 3 | [**struct tm *localtime(const time_t *time);**](c-function-localtime.html) This function returns a pointer to a **tm** structure representing local time. | | 4 | [**clock_t clock(void);**](c-function-clock.html) This function returns the processor clock time used since the program started (generally at the program's beginning). If time is unavailable, it returns -1. | | 5 | [**char * asctime ( const struct tm * time );**](c-function-asctime.html) This function returns a pointer to a string containing the information stored in the structure pointed to by time, in the form: day month date hours:minutes:seconds year\n\0. | | 6 | [**struct tm *gmtime(const time_t *time);**](c-function-gmtime.html) 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);**](c-function-mktime.html) This function returns calendar time equivalent to the time stored in the structure pointed to by time. | | 8 | [**double difftime ( time_t time2, time_t time1 );**](c-function-difftime.html) This function returns the number of seconds difference between time1 and time2. | | 9 | [**size_t strftime();**](c-function-strftime.html) This function can be used to format date and time into a specified format. |

    Current Date and Time

    The example below 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

    (

    )

    {

    // Based on the current date/time of the 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 a tm structure

    tm

    gmtm

    =

    gmtime

    (

    &

    now

    )

    ;

    dt

    =

    asctime

    (

    gmtm

    )

    ;

    cout

    <<

    "

    UTC date and time:

    "

    <<

    dt

    <<

    endl

    ;

    }

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

    
    
    本地日期和时间:Sat Jan  8 20:07:41 2011
    
    
    
    UTC 日期和时间:Sun Jan  9 03:07:41 2011
    
    

    Formatting Time Using the tm Structure

    tm The structure is particularly important when handling date- and time-related operations in C/C++. The tm structure stores 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 with structures, you need a basic understanding of C structures and how to use the arrow -> operator to access structure members.

    Example

    #include

    <
    
    iostream
    
    #include
    
    <
    
    ctime
    
    using
    
    namespace
    
    std

    ;

    int

    main

    (

    )

    {

    // Based on the current date/time of the system

    time_t

    now

    =

    time

    (

    0

    )

    ;

    cout

    <<

    "

    Seconds elapsed since 1970:

    "

    <<

    now

    <<

    endl

    ;

    tm

    ltm

    =

    localtime

    (

    &

    now

    )

    ;

    // Output each component 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 code above is compiled and executed, it produces the following result:

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