C++ Get the current local time with ctime
Posted by Samath
Last Updated: January 04, 2017

The ctime header file contains definitions of functions to get and manipulate date and time information.

Code:

#include <iostream>
#include <ctime>

int main()
{
  time_t thecurrTime;
  struct tm *thelocTime;

  time( &thecurrTime );                   
  thelocTime = localtime( &thecurrTime ); 

  int Day    = thelocTime->tm_mday;
  int Month  = thelocTime->tm_mon + 1;
  int Year   = thelocTime->tm_year + 1900;
  int Hour   = thelocTime->tm_hour;
  int Min    = thelocTime->tm_min;
  int Sec    = thelocTime->tm_sec;

  std::cout << "Execution Time: " << Hour << ":" << Min << ":" << Sec << std::endl;
  std::cout << "Current Time: " << Day << "/" << Month << "/" << Year << std::endl;
  return 0;
}