×

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The < ctime > header file is

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The < ctime > header file is included in the application to utilize date and time in C++.

The following are the four time-related types in this header file:

  • The t-clock is an arithmetic type name and represents a type of clock. Represents a cold number in the clock (fixed time units with a certain system length). Retrieved type (clock /) t.
  • Time t is an abbreviated form of time. Represents the time returned by the time function (). If the time exceeds 00:00 hours, we subtract the total value as the number of seconds exceeds.
  • Size t indicates the size of any object by bytes and is a nickname for the type of the unsigned number. Task size () prints size and calculation, and output is size t.
  • tm - In the C structure, the tm structure saves the day and time. Described as follows-
struct time {  
    int time_sec; // seconds of minutes from 0 to 61  
    int time_min; // minutes of hour from 0 to 59  
    int time_hour; // hours of day from 0 to 24  
    int time_monday; // day of month from 1 to 31  
    int time_month; // month of year from 0 to 11  
    int time_year; // year since 1900  
    int time_wday; // days since sunday  
    int time_yday; // days since January 1st  
    int time_isdst; // hours of daylight savings time  
}  

C++ date and time functions

The function nameThe function prototypeThe function description
mktimetime_t mktime ( struct time *tm ) ;This function converts mktime to time t, which is the calendar date and time format.
ctimechar *ctime ( const time_t *tm ) ;It gives you a reference to a string with the following format: day month year hours: minutes: seconds year.
difftimedouble difftime ( time_t time2, time_t time1 ) ;The difference between two time objects t1 and t2 is returned.
gmtimestruct time *gmtime ( const time_t *time ) ;This method returns a reference to the time in structure format. The time is in UTC (Universal Coordinated Time).
clockclock_t clock ( void ) ;It offers a rough estimate of how long the caller application has been running. If the value is not accessible, the value.1 is returned.
localtimestruct time *localtime ( const time_t *time ) ;The pointer to the tm structure, which represents local time, is returned by this method.
timetime_t time ( time_t *time ) ;It is the current time.
strftimesize_t strftime ( ) ;We may format date and time in a certain way with the aid of this function.
asctimechar * asctime ( const struct tm * time ) ;The function transforms the tm type object to a string and returns the string's pointer.

The example below shows how to print the current date and time in UTC format:

#include < ctime >   
#include < iostream >  
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio >
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  
    return 0;
}

OUTPUT:

The local date and time is: Wed Jun 15 20:19:40 2022
The UTC date and time is: Wed Jun 15 20:19:40 2022
………………………………………………………………………………..
Process executed in 2.33 seconds
Press any key to continue.

Explanation:

In the above example of a program in C++, we have used a header file named ctime to find the current date and time, time_t get our current date and time with re4spect to the system.

The code below shows how to split the tm structure and output each attribute separately using the -> operator:

#include <iostream>  
#include <ctime>  
using namespace std;  
int main()  
{  
    time_t now = time(0); // get current date and time  
    cout << "Number of seconds sinceJune 1, 2022 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;  
    return 0;
}  

OUTPUT:

Number of seconds since June 1, 2022 is:: 1632328
Year:2022
Month: 6
Day: 15
Time: 22:40:53
................................................
Process executed in 2.22 seconds
Press any key to continue.

Explanation:

In the above example of a code in C++, we have shown how to split the tm structure and output each attribute separately using the -> operator.

included in the application to utilize date and time in C++.

The following are the four time-related types in this header file:

  • The t-clock is an arithmetic type name and represents a type of clock. Represents a cold number in the clock (fixed time units with a certain system length). Retrieved type (clock /) t.
  • Time t is an abbreviated form of time. Represents the time returned by the time function (). If the time exceeds 00:00 hours, we subtract the total value as the number of seconds exceeds.
  • Size t indicates the size of any object by bytes and is a nickname for the type of the unsigned number. Task size () prints size and calculation, and output is size t.
  • tm - In the C structure, the tm structure saves the day and time. Described as follows-
struct time {  
    int time_sec; // seconds of minutes from 0 to 61  
    int time_min; // minutes of hour from 0 to 59  
    int time_hour; // hours of day from 0 to 24  
    int time_monday; // day of month from 1 to 31  
    int time_month; // month of year from 0 to 11  
    int time_year; // year since 1900  
    int time_wday; // days since sunday  
    int time_yday; // days since January 1st  
    int time_isdst; // hours of daylight savings time  
}  

C++ date and time functions

The function nameThe function prototypeThe function description
mktimetime_t mktime ( struct time *tm ) ;This function converts mktime to time t, which is the calendar date and time format.
ctimechar *ctime ( const time_t *tm ) ;It gives you a reference to a string with the following format: day month year hours: minutes: seconds year.
difftimedouble difftime ( time_t time2, time_t time1 ) ;The difference between two time objects t1 and t2 is returned.
gmtimestruct time *gmtime ( const time_t *time ) ;This method returns a reference to the time in structure format. The time is in UTC (Universal Coordinated Time).
clockclock_t clock ( void ) ;It offers a rough estimate of how long the caller application has been running. If the value is not accessible, the value.1 is returned.
localtimestruct time *localtime ( const time_t *time ) ;The pointer to the tm structure, which represents local time, is returned by this method.
timetime_t time ( time_t *time ) ;It is the current time.
strftimesize_t strftime ( ) ;We may format date and time in a certain way with the aid of this function.
asctimechar * asctime ( const struct tm * time ) ;The function transforms the tm type object to a string and returns the string's pointer.

The example below shows how to print the current date and time in UTC format:

#include < ctime >   
#include < iostream >  
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio >
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  
    return 0;
}

OUTPUT:

The local date and time is: Wed Jun 15 20:19:40 2022
The UTC date and time is: Wed Jun 15 20:19:40 2022
………………………………………………………………………………..
Process executed in 2.33 seconds
Press any key to continue.

Explanation:

In the above example of a program in C++, we have used a header file named ctime to find the current date and time, time_t get our current date and time with re4spect to the system.

The code below shows how to split the tm structure and output each attribute separately using the -> operator:

#include <iostream>  
#include <ctime>  
using namespace std;  
int main()  
{  
    time_t now = time(0); // get current date and time  
    cout << "Number of seconds sinceJune 1, 2022 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;  
    return 0;
}  

OUTPUT:

Number of seconds since June 1, 2022 is:: 1632328
Year:2022
Month: 6
Day: 15
Time: 22:40:53
................................................
Process executed in 2.22 seconds
Press any key to continue.

Explanation:

In the above example of a code in C++, we have shown how to split the tm structure and output each attribute separately using the -> operator.


Related Topics

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.