Am writing code to split seconds into years, days, minutes, seconds. I’m getting some unexpected results: eg 3600 seconds comes out as 1 hour. But 3599 seconds comes out as 60 minutes and 59 seconds.
Firstly, there should never be 60 minutes. This should be 1 hour. But that aside the actual result is wrong.
Is there some error in my calculation logic, or is there a funky rounding error going on using modulo and Math.floor? Thanks!
function convertFromSeconds(time) {
const minute = 60
const hour = 60 * minute; //3600
const day = 24 * hour; // 86400
const year = 365 * day //31536000
const years = Math.floor(time / year)
const days = Math.floor((time % year) / day)
const hours = Math.floor((time % day) / hour)
const minutes = Math.floor((time % hour) / minute)
const seconds = time % minute;
// Rest of code formats the above result into a string for output
}