How to get current time and date in C++?


Here is an example to get current date and time in C++ language,

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   time_t now = time(0);
   char *date = ctime(& now);

   cout << "The local date and time : " << date << endl;
}

Output

Here is the output

The local date and time : Wed Oct 3 08:35:24 2018

In the above program, the code to get current date and time is present in main(). Here, time_t is the return type of variable now. In-built function time() is used to get the local current time and date.

time_t now = time(0);
char *date = ctime(& now);

Updated on: 25-Jun-2020

719 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements