Hello, I'll try to be as educational as possible so that more people can take advantage of this response in the future.
Although you CAN start by selecting the minutes, then the hours and then the days, it is usually easier to do the reverse. The diagram shows the general idea of how to transform from a smaller unit (in your case seconds ) to larger units (in your case, minutes , in> and days ).
Theideaisthat,withtheinputmeasuredinthesmallestunit(seconds),thealgorithmfindsina1ststephowmanytimesthelargestunitinput-onthediagram,are3times.Theninstep2,theprogramwillfindhowmanytimesthesecondlargestunit(hours)fitswithintherestofthe1ststep,andsoonuntilthedesiredaccuracyisreached.
Animplementationofthisalgorithmis in this jsFiddle , with several comments to aid understanding.
Two dangerous details I noticed in your code were:
- Math.round () : rounds to the nearest integer, either greater or smaller . For example: 1,5 and 1,598 and would be rounded to 2; since 1.499 and 1.0 would be rounded to 1. Therefore, I found the use of this function dangerous, because to eliminate surpluses in each step, we do not want the algorithm to go up in any case. I suggest using Math.floor () , which always rounds down.
-
Although you find out how many minutes remain, you use the total minutes to get the times , and the same thing repeats for the hours in relation to the days . Suppose, for example, that the total number of hours is 15 (% with%). According to its algorithm, the remaining hours (% with%) would be 15 (which is correct), but the number of days (% with%) would be equal to todas_hrs=15
which results in ... 1 !!!
By adjusting these details, you will realize that it is not impossible to do your method, but you need more attention, which is why I recommend using the most popular algorithm, which I indicated, okay?
I hope I have helped! Hugs!