Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to format time hours-minutes-seconds in C++

2 Answers

0 votes
#include <iostream>
#include <iomanip> // For std::setw and std::setfill

int main() {
    int hours = 9, minutes = 36, seconds = 7;

    // Format and print time as HH:MM:SS
    std::cout << std::setw(2) << std::setfill('0') << hours << ":"
              << std::setw(2) << std::setfill('0') << minutes << ":"
              << std::setw(2) << std::setfill('0') << seconds << std::endl;
}



/*
run:

09:36:07

*/

 



answered Jul 22, 2025 by avibootz
0 votes
#include <iostream>
#include <iomanip>
#include <ctime>

int main() {
    // Create a tm struct with the desired date and time
    std::tm date = {};
    date.tm_year = 2024 - 1900; // Years since 1900
    date.tm_mon  = 12 - 1;      // Months since January
    date.tm_mday = 10;
    date.tm_hour = 15;
    date.tm_min  = 21;
    date.tm_sec  = 42;

    // Format the time as HH:mm:ss
    std::cout << std::put_time(&date, "%H:%M:%S") << std::endl;
}




/*
run:

15:21:42

*/

 



answered Jul 22, 2025 by avibootz

Related questions

1 answer 67 views
1 answer 63 views
1 answer 60 views
1 answer 65 views
1 answer 65 views
1 answer 71 views
1 answer 71 views
...