Display current Date and Time in C++

To show Date and Time in a C++ program we have used the “localtime()” function.

Calculation of day, month and year is explained within the program itself.

 

Program to display current Date and Time C++:

#include <iostream>
#include <time.h>
using namespace std;

time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
int day = aTime->tm_mday;
int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int year = aTime->tm_year + 1900; // Year is # years since 1900


int main()
{

    cout<< "Current Date: ";
    cout<<day << " "<<month<<" "<<year;
    return 0;
}

OUTPUT:

Current Date: 15 12 2020

current date and time

Leave a Comment