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

To get the current date and time in c++ use time() method:

Example

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

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

Output

The local date and time : Sun Jun 19 09:30:28 2022

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

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

kabeer

Added  Jun 19, 04:06 pm

Article tags

×
Google login
SignUp with Email
X