Skip to content
Related Articles

Related Articles

C++ Program to print current Day, Date and Time
  • Difficulty Level : Medium
  • Last Updated : 31 Jul, 2017

In order to facilitate finding the current local day, date and time, C++ has defined several functions in the header file , so functions which will help us in achieving our objective of finding local day, date and time are:

time():

  • It is used to find the current calendar time.
  • Its return type is time_t, which is an arithmetic data type capable of storing time returned by this function.
  • If its argument is not NULL, then it assigns its argument the same value as its return value.

localtime():

  • It uses the argument of time(), which has the same value as the return value of time(), to fill a structure having date and time as its components, with corresponding time in local timezone.

asctime():

  • It is used to convert the contents in the structure filled by localtime into a human-readable version which finally returns day, date and time in the given format:
    Day Month Date hh:mm:ss Year
    




// C++ program to find Current Day, Date
// and Local Time
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    // Declaring argument for time()
    time_t tt;
  
    // Declaring variable to store return value of
    // localtime()
    struct tm * ti;
  
    // Applying time()
    time (&tt);
  
    // Using localtime()
    ti = localtime(&tt);
  
    cout << "Current Day, Date and Time is = " 
         << asctime(ti);
  
  return 0;
}

Output:



It will show the current day, date and localtime, in the
format Day Month Date hh:mm:ss Year

Points to remember:

  1. This program will give output different for different time zones as per the time in that time zone.
  2. The Day, Date and Time in the output is independent of the system day, date and time. You can change your system date and time settings, but still the output will not be affected, and will give the correct information.

This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :