Created
May 14, 2019 02:29
-
-
Save matiaslopezd/0451899fa72794a12fdbe1d15bad1880 to your computer and use it in GitHub Desktop.
Seconds in string "00:00:00" format
This file contains 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 timeFormat = (time) => { | |
// Time is in seconds | |
time = time || 0; | |
let seconds = time % 60; | |
let minutes = (time / 60) % 60; | |
const hours = (time / 60) / 60; | |
const numberToString = (value) => { | |
value = Math.trunc(value); | |
return (value < 10) ? `0${value}` : value; | |
} | |
seconds = (seconds < 60) ? seconds : 0; | |
minutes = (minutes < 60) ? minutes : 0; | |
// Return in object format | |
return `${numberToString(hours)}:${numberToString(minutes)}:${numberToString(seconds)}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo: https://codepen.io/matiaslopezd/pen/zQreVQ