Current Date and Time in C++ - C++ Program to display current date and time

Published January 22, 2021


In this cpp date example we will learn how to  display current date and time.

For this example we import two packages

#include<cmath>
#include <ctime>

 

Examples

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

int main()
{

    time_t t = time(NULL);
    tm* timePtr = localtime(&t);

    cout << "seconds= " << (timePtr->tm_sec) << endl;
    cout << "minutes = " << (timePtr->tm_min) << endl;
    cout << "hours = " << (timePtr->tm_hour) << endl;
    cout << "day of month = " << (timePtr->tm_mday) << endl;
    cout << "month of year = " << (timePtr->tm_mon)+1 << endl;
    cout << "year = " << (timePtr->tm_year)+1900 << endl;
    cout << "weekday = " << (timePtr->tm_wday )<< endl;
    cout << "day of year = " << (timePtr->tm_yday )<< endl;
    cout << "daylight savings = " <<(timePtr->tm_isdst )<< endl;
    cout << endl;
    cout << endl;


    cout << "Date     " <<(timePtr->tm_mday)<<"/"<< (timePtr->tm_mon)+1 <<"/"<< (timePtr->tm_year)+1900<< endl;
    cout << "Time     " << (timePtr->tm_hour)<<":"<< (timePtr->tm_min)<<":"<< (timePtr->tm_sec) << endl;
    return 0;
}

 

This will return below out for the current date

 

seconds= 36                                                                                                                            
minutes = 45                                                                                                                           
hours = 7                                                                                                                              
day of month = 22                                                                                                                      
month of year = 1                                                                                                                      
year = 2021                                                                                                                            
weekday = 5                                                                                                                            
day of year = 21                                                                                                                       
daylight savings = 0                                                                                                                   
                                                                                                                                       
                                                                                                                                       
Date     22/1/2021                                                                                                                     
Time     7:45:36 

 

 

Time manipulation
difftime
computes the difference between times
(function)
time
returns the current time of the system as time since epoch
(function)
clock
returns raw processor clock time since the program is started
(function)
timespec_get
(C++17)
returns the calendar time based on a given time base
(function)
 
Format conversions
asctime
converts a tm object to a textual representation
(function)
ctime
converts a time_t object to a textual representation
(function)
strftime
converts a tm object to custom textual representation
(function)
wcsftime
converts a tm object to custom wide string textual representation
(function)
gmtime
converts time since epoch to calendar time expressed as Universal Coordinated Time
(function)
localtime
converts time since epoch to calendar time expressed as local time
(function)
mktime
converts calendar time to time since epoch

As a beginner programmer you should also know about how to Conver String to Char Array using C++

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

415 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions