How To: Get the Current Date and Time in C++

Here is some sample code for use in a C++ program.

// timetest.cpp -- sample program to get current time and date

#include <iostream>
using std::cout;
using std::endl;

#include <ctime>

int main()
{
  time_t t;
  time(&t);
  cout << ctime(&t) << endl; // e.g., Fri May 02 17:57:14 2003
  cout << t << endl; // #seconds since 12/31/1969, e.g.: 1051923434

  return 0;
}