C++ Date And Time - C++ Programming Tutorial
C++ Course / Miscellaneous / C++ Date And Time

C++ Date And Time

BLUF: Mastering C++ Date And Time is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Date And Time

C++ is renowned for its efficiency. Learn how C++ Date And Time enables low-level control and high-performance computing in the tutorial below.

In this tutorial, we'll explore the date and time formats in C++. C++ does not have a built-in date and time format, so it relies on inheriting it from the C language. To work with date and time in C++, the <ctime> header file is included in the program.

__PRESERVE_6__

This header file has four time-related types as follows -

  • Clockt - It stands for clock type which is an alias of arithmetic type. It represents clock tick counts(units of a time of a constant with system-specific length). Clockt is the type returned by clock/.
  • Timet - It stands for timetype. It represents the time that is returned by function time. It outputs an integral value as the number of seconds elapsed when time passes by 00:00 hours.
  • Sizet - It is an alias for an unsigned integer type and represents the size of any object in bytes. Sizet is the result of the sizeof operator which prints sizes and counts.
  • tm - The tm structure holds date and time in the C structure. It is defined as follows -
Example

struct tm {
    int tm_sec; // seconds of minutes from 0 to 61
    int tm_min; // minutes of hour from 0 to 59
    int tm_hour; // hours of day from 0 to 24
    int tm_mday; // day of month from 1 to 31
    int tm_mon; // month of year from 0 to 11
    int tm_year; // year since 1900
    int tm_wday; // days since sunday
    int tm_yday; // days since January 1st
    int tm_isdst; // hours of daylight savings time
}

Date and time functions in c++

Name of the function Prototype of the function Description About the function
mktime time_t mktime(struct tm *time); This function converts mktime to time_t or calendar date and time.
ctime char ctime(const time_t time); It returns the pointer to a string of the format - day month year hours: minutes: seconds year.
difftime double difftime ( timet time2, timet time1 ); It returns the difference of two-time objects t1 and t2.
gmtime struct tm gmtime(const time_t time); This function returns the pointer of the time in the format of a structure. The time is in UTC.
clock clock_t clock(void); It returns an approximated value for the amount of time the calling program is being run. The value .1 is returned if not available.
localtime struct tm localtime(const time_t time); This function returns the pointer to the tm structure representing local time.
time timet time(timet *time); It represents current time.
strftime size_t strftime(); With the help of this function, we can format date and time in a specific manner.
asctime char asctime ( const struct tm time ); The function converts the type object of tm to string and returns the pointer to that string.

Example to print current date and time

Below is a sample code snippet that demonstrates how to display the present date and time in the Coordinated Universal Time (UTC) format.

Example

#include <ctime> 
#include <iostream>

using namespace std;

int main()
{

    time_t now = time(0); // get current dat/time with respect to system

    char* dt = ctime(&now); // convert it into string

    cout << "The local date and time is: " << dt << endl; // print local date and time

    tm* gmtm = gmtime(&now); // for getting time to UTC convert to struct
    dt = asctime(gmtm);
    cout << "The UTC date and time is:" << dt << endl; // print UTC date and time
}

Output

Output

The local date and time is: Wed Sep 22 16:31:40 2021

The UTC date and time is: Wed Sep 22 16:31:40 2021

The following code demonstrates how to access and display each attribute of the tm structure by utilizing the -> operator.

Example

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    time_t now = time(0); // get current date and time

    cout << "Number of seconds since January 1,2021 is:: " << now << endl;

    tm* ltm = localtime(&now);

    // print various components of tm structure.
    cout << "Year:" << 1900 + ltm->tm_year << endl; // print the year
    cout << "Month: " << 1 + ltm->tm_mon << endl; // print month number
    cout << "Day: " << ltm->tm_mday << endl; // print the day
    // Print time in hour:minute:second
    cout << "Time: " << 5 + ltm->tm_hour << ":";
    cout << 30 + ltm->tm_min << ":";
    cout << ltm->tm_sec << endl;
}

Output

Output

Number of seconds since January 1,2021 is:: 1632328553
Year:2021
Month: 9
Day: 22
Time: 21:65:53

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience