Dear Sir/Mam,
I want to display only current hours, Minutes and Seconds through cpp Computer graphics program. ctime function produces the current time with date day etc. Is there any CPP function, which gives me results only the current time in form HH:MM:SS?
No, but you can like this:
#include <iostream>
#include <ctime>
using namespace std;
string hh_mm_ss(time_t time) {
tm *now = localtime(&time);
string result;
if (now->tm_hour < 10)
result.push_back('0');
result += to_string(now->tm_hour);
result.push_back(':');
if (now->tm_min < 10)
result.push_back('0');
result += to_string(now->tm_min);
result.push_back(':');
if (now->tm_sec < 10)
result.push_back('0');
result += to_string(now->tm_sec);
return result;
}
int main() {
time_t now = time(0);
cout << hh_mm_ss(now);
return 0;
}
Comments
Leave a comment