Skip to content

Instantly share code, notes, and snippets.

@matiaslopezd
Created May 14, 2019 02:29
Show Gist options
  • Save matiaslopezd/0451899fa72794a12fdbe1d15bad1880 to your computer and use it in GitHub Desktop.
Save matiaslopezd/0451899fa72794a12fdbe1d15bad1880 to your computer and use it in GitHub Desktop.
Seconds in string "00:00:00" format
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)}`;
};
@matiaslopezd
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment