Thursday, December 13, 2012

C++ TUTORIAL FOR BEGINNERS: How to capture the system date and time and display them in useful common format


Source code:

/*
PROGRAM: Capturing the System date and time in business & military format using C++ language
AUTHOR: eternaltreasures
DATE: 2010 September 26
*/

#include <iostream>
#include <time.h>
using namespace std;

int main()
{

/*
Option 1: Capturing the system date using the time () function
ctime format is DayOfTheWeek Month Day Hour:Minute:Seconds Year
*/

time_t systemtime;
time(&systemtime); 

cout << endl;
cout << "System date and time is: " << ctime(&systemtime);
cout << endl;   
   
/*
Option 2: Capturing the system date and time using strdate, strtime
System date format mm/dd/yy (month/day/year)
System time format hh/mm/ss (hour:minute:seconds)
*/

char sysdate[9];
char systime[9];

_strdate(sysdate);
_strtime(systime);

cout << "System time: " << systime << endl;
cout << "System date: " << sysdate << endl;

return 0;
}

No comments:

Post a Comment