Date and Time in C++

In this tutorial, we will learn about the date and time in  C++.

Date and Time in C++

For various programs, we need to access the time and the date, along with other calendar-related details. For this, we have the following header file which defines all time-related functions.

#include <ctime>

This library is inherited from C and contains all dates and time manipulation functions and structures in it.

Also read:

There are four time-related types: time_t, clock_t, size_t, and tm. Here, tm represents a structure which contains the following :

struct tm {
   int tm_sec;   // seconds of minutes from 1 to 60
   int tm_min;   // minutes of hour from 1 to 60
   int tm_hour;  // hours of day from 1 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
}

Current Local Dates and Time in C++

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

int main()
{
  time_t now = time(0);
  char* date = ctime(&now);
  cout << " seconds till now = " << now << endl;
  cout << "today's date and time is = " << date << endl;

  return 0;
}

Output :

seconds till now = 1571072005
today's date and time is = Mon Oct 14 22:23:25 2019

Explanation :

time_t, as told previously, is the time type. It is implemented generally as the time elapsed since Jan 1, 1970, in seconds. Hence, in our variable now, we get the seconds that have elapsed till now since then. When we use the pointer for the date and pass the address of now, we get a pointer to a string of the form day month date hours: minutes: seconds year.

tm structure

We saw how to access simply the date and time through the time_t. The structure tm described above can also be used as it stores so much information regarding dates and time.

Now, here is a program to depict how to access date and time using tm structure and above functions.

Program to show date and time in C++

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

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

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

  tm* lt = localtime(&now);

  cout << "Year : " << 1900 + lt->tm_year << endl;      
  cout << "Month: " <<1+ lt->tm_mon << endl;
  cout << "Day: " << lt->tm_mday << endl;
  cout << "Time: " << lt->tm_hour << ":";
  cout << lt->tm_min << ":";
  cout << lt->tm_sec << endl;

  return 0;
}

Output :

Number of seconds since January 1,1970 : 1571151618
Year : 2019
Month: 10
Day: 15
Time: 20:30:18

Explanation :

In the above program, we first print out the number of seconds passed since Jan 1, 1970. This is similar to the previous program. then we use the tm structure to access the year. Here, we added 1900 because this structure calculates from 1900 and gives the value. Thus, adding 1900 gives the correct value of the current year. Similarly, for months we add 1 because the tm structure stores month number from 0 to 11. The rest of the value we get in the same way.

Hope this was helpful. Enjoy Coding!

Also learn :

Bitwise Operators in C or C++

log() function in C++

Leave a Reply

Your email address will not be published. Required fields are marked *