How to get date in C++
C++ has defined various functions in the header file to make it easier to determine the current date. Because C++ lacks a proper date and time format, we must rely on the C language. To use date C++, include the header file in the program.
The functions that will assist us achieve our goal of determining the date are:
time()
The time() function returns the current calendar time.
Its return type is time_t, an arithmetic data type capable of recording the time returned by this method.
If the argument is not NULL, it is assigned the same value as the return value.
Calendar dates are also presented on the screen alongside the current day, date, and time. The ctime library contains all of C++'s date and time-related functions and variables.
localtime()
It fills a structure with date and time as its components with the corresponding time in the local timezone using the time() argument, which has the same value as the function's return result.
Because C++ lacks a proper date and time format, we must rely on the C language. To use date and time in C++, include the header file in the program.
This header file contains four time-related types, as follows:
Clock_t: Clock_t is a synonym for arithmetic type. It represents clock tick counts (units of time for a constant of system-specific length). Clock_t is the data type returned by clock()/.
Time_t: Time_t stands for time_type. It represents the time returned by the function time(). It returns an integral value representing the number of seconds elapsed when the time reaches 00:00 hours.
Size_t: Size_t is an alias for an unsigned integer type that specifies the size of an object in bytes. Size_t is the output of the sizeof() operation, which displays sizes and counts.
tm: The tm structure contains the date and time in the C structure. The definition is as follows:
struct tm {
int tm_seconds;
int tm_minutes;
int tm_hours;
int tm_monthday;
int tm_month;
int tm_year;
int tm_weekday;
int tm_yearday;
int tm_isdst;
}
seconds: represents seconds (0-61)
minutes: represents minutes (0-59)
hours: represents hours (0-24)
monthday: represents day of the month (1-31)
month: represents month (0-11, where 0 is January)
year: represents year since 1900
weekday: represents day of the week (0-6, where 0 is Sunday)
yearday: represents the day of the year (0-365, where 0 is January 1st)
isdst: represents Daylight Saving Time flag (positive if DST is in effect, 0 if not, negative if unknown)
Functions for the date in C++
- clock: It returns an approximation of the time the caller program is running. If the value is not available, it is returned as 1.
- difftime: It gives the variation between two-time objects (t1 and t2).
- ctime: It returns a pointer to a string of the format day, month year, hours: minutes: seconds, year.
- asctime: The function transforms tm's kind object to a string and provides a reference to that string.
Example of printing the current date
The following is an example of printing the current date in UTC format.
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
time_t present = time(0); // retrieve the current date/time for the system.
char* date = ctime(&present); // transform it into a string.
cout << "The current local date and time is: " << date << endl; // date and time are printed
tm* xyz = gmtime(&present); // Convert time to UTC to struct.
date = asctime(xyz);
cout << "The UTC time and date are:" << date << endl; // UTC date and time are printed
}
Output:
![How to get date in C++/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p>The code below demonstrates how to break the tm structure and print each attribute independently using the -> operator.</p>
<!-- /wp:paragraph -->
<!-- wp:preformatted -->
<pre class=](https://static.tutorialandexample.com/cpp/how-to-get-date-in-cpp1.png)
#include <ctime>
using namespace std;
int main()
{
time_t present = time(0);
cout << "The total number of seconds from January 1, 2024, is: " << present << endl;
tm* timelcl = localtime(&present);
cout << "Year:" << 1900 + timelcl->tm_year << endl; // print the year
cout << "Month: " << 1 + timelcl->tm_mon << endl; // print month number
cout << "Day: " << timelcl->tm_mday << endl; // print the day
cout << "Time: " << 5 + timelcl->tm_hour << ":";
cout << 30 + timelcl->tm_min << ":";
cout << timelcl->tm_sec << endl;
}
Output:
![How to get date in C++/>
<!-- /wp:html -->
<!-- wp:html -->
<div class=](https://static.tutorialandexample.com/cpp/how-to-get-date-in-cpp2.png)