Program to print Date in C++

Program to print Date


  1. Create a variable t_ti of type time which stores the time from Jan 1,1970
  2. Create a pointer datePtr which stores the calendar dates.
  3. Print the date, day and year using datePtr.


#include <iostream>
#include <ctime>

using namespace std;

int main() {
    time_t ti = time(NULL);
    tm* datePtr = localtime(&ti);

    cout << "Date: " << (datePtr->tm_mday) << "/" << (datePtr->tm_mon) + 1 << "/" << (datePtr->tm_year) + 1900 << endl;

    return 0;

Use of terms used:

The time() function in C++ returns the current calendar time as an object of type time_t.


struct tm * localtime (const time_t * timer);

Convert time_t to tm as local time

Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed for the local timezone.

Program to print Date in C++