Answer to Question #199452 in C++ for Anupama Kota

Question #199452

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?


1
Expert's answer
2021-05-27T19:09:12-0400

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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog