In: Computer Science
Write a C++ program that displays the current time
Program:
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int hours,mins,sec;
string amOrpm;
time_t currentDateAndTime;
struct tm * getTime;
time (¤tDateAndTime); //get the current
system date and time
getTime = localtime (¤tDateAndTime); //store
the time in struct variable
hours=getTime->tm_hour; //extract hours from
time
mins=getTime->tm_min; //extract mintues from
time
sec=getTime->tm_sec; //extract seconds from
time
//if hours is greater than 12 set time as PM
if(hours>12)
{
hours=hours-12;
amOrpm="PM";
}
//else set time as AM
else
amOrpm="AM";
//display the time
cout<<"\nThe current time is
"<<hours<<":"<<mins<<":"<<sec<<"
"<<amOrpm<<"\n";
return 0;
}
Output: