简体   繁体   中英

How to change seconds into HH:MM:SS format in C++?

So I have a function that returns the seconds between the range 0 and 86400 (24 hours). My goal is to take those seconds and return the hours, remaining minutes (between 0 and 60), and remaining seconds (between 0 and 60).

// What i have tried:

    hour = timeInSeconds / 3600;
    min = timeInSeconds / 60; // this is returning total minutes (which i dont want)
    sec = timeInSeconds % 60;

The main problem im having is with the minutes because they go above 60.

For example, if i make the time = 35000 seconds, i will get the hours to be 9 but my minutes will be around 853.

Therefore this is how my output looks like:

17:0853:33 // How can i get remaining minutes? This returns to me total minutes.

So i guess, What im asking is how do i get the left over minutes from the total seconds after the hours have been calculated?

min = ((timeInSeconds % 3600) / 60)

Just needed to think a little bit more

hour = timeInSeconds / 3600;
min = (timeInSeconds - hour * 3600) / 60;
sec = timeInSeconds  - hour * 3600 - min * 60;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM