Program to print date

0 Suresh Chand March 30, 2021

In this example, We will learn to print date using c++ programming language. Here we will use ctime header file.

Lets look an code to make clear concepts

CODE to Print Date:

#include <iostream>

#include <ctime>

using namespace std;

int main()

{

    time_t ti = time(NULL);

    tm *datePtr = localtime(&ti);

    cout << "Year : " << (datePtr->tm_year + 1900) << endl;
    cout << "Month : " << datePtr->tm_mon + 1 << endl;
    cout << "Day : " << datePtr->tm_mday << endl;
    cout << "Hour : " << datePtr->tm_hour << endl;
    cout << "Minute : " << datePtr->tm_min << endl;
    cout << "Second : " << datePtr->tm_sec << endl;

    //printing today date
    cout << "\nToday Date : " << datePtr->tm_year+1900 << "/" << datePtr->tm_mon+1 << "/" << datePtr->tm_mday << endl;

    return 0;
}

The output of above programming is

Year : 2021
Month : 3
Day : 29
Hour : 20
Minute : 15
Second : 50

Today Date : 2021/3/29

Explanation:

In this example time() function will return current calendar time object. When you print time_obj variable then you will get a like this : 1617027027

datePtr pointer variable will return an object of date. You can access the following parameter from datePtr variable.

  • tm_year: Return current year
  • tm_yday: Return day
  • tm_wday: Return days since Sunday (0-6). 0 stands for Sunday and 6 stands for Saturday
  • tm_mon: Return month since January (0-11)
  • tm_mday: Return day from (0-31)
  • tm_hour: Hours since midnight: 0-23
  • tm_min: Minutes: 0-59

For more details you can check more details about ctime header file.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

Ads Block Detector Powered by codehelppro.com

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System