Gallanari
Gallanari

Reputation: 45

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?

Upvotes: 0

Views: 1413

Answers (2)

Gallanari
Gallanari

Reputation: 45

min = ((timeInSeconds % 3600) / 60)

Just needed to think a little bit more

Upvotes: 2

Onyambu
Onyambu

Reputation: 79188

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

Upvotes: 0

Related Questions