C++ Code to display Date & Time.

Code : 

#include <iostream>
#include <ctime>

using namespace std;

int main( )
{
   // current date/time based on current system
   time_t now = time(0);

   cout << "Number of sec since January 1,1970:" << now << endl;

   tm *ltm = localtime(&now);

   // print various components of tm structure.
int yr =  1900 + ltm->tm_year;
  //cout << "Year: "<< 1900 + ltm->tm_year << endl;
   int mnt = 1 + ltm->tm_mon;
  // cout << "Month: "<< 1 + ltm->tm_mon<< endl;
  int ddy = ltm->tm_mday;
   //cout << "Day: "<<  ltm->tm_mday << endl;
cout << "\nyear:" << yr << "\nmonth:" << mnt << "\nday:" << ddy << "\n";

   cout << "Time: "<< 1 + ltm->tm_hour << ":";
   cout << 1 + ltm->tm_min << ":";
   cout << 1 + ltm->tm_sec << endl;
}


Output :

Number of sec since January 1,1970:1388472083

year:2013
month:12
day:31
Time: 13:12:24

Comments