Created
May 11, 2021 22:28
-
-
Save mrv1k/292ed6cd2568a5924708b9f86e04bf16 to your computer and use it in GitHub Desktop.
2 ways to convert seconds to time object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MINUTE = 60; | |
const HOUR = MINUTES * 60; | |
const DAY = HOUR * 24; | |
function secondsToTime1(timestamp) { | |
let time = timestamp; | |
const days = Math.floor(time / DAY); | |
time -= days * DAY; | |
const hours = Math.floor(time / HOUR); | |
time -= hours * HOUR; | |
const minutes = Math.floor(time / MINUTE); | |
time -= minutes * MINUTE; | |
const seconds = time; | |
return { days, hours, minutes, seconds }; | |
} | |
function secondsToTime2(timestamp) { | |
const days = Math.floor(timestamp / DAY); | |
const hours = Math.floor((timestamp % DAY) / HOUR); | |
const minutes = Math.floor((timestamp % HOUR) / MINUTES); | |
const seconds = Math.floor((timestamp % HOUR) % MINUTES); | |
return { days, hours, minutes, seconds }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment