Skip to content

Instantly share code, notes, and snippets.

@soggybag
Last active November 8, 2017 19:04
Show Gist options
  • Save soggybag/458dc218bf62622b9d990a69ef24eb41 to your computer and use it in GitHub Desktop.
Save soggybag/458dc218bf62622b9d990a69ef24eb41 to your computer and use it in GitHub Desktop.
Display time as 00:00:00 or 09:26:34
padWithZeros(n) {
return n > 9 ? `${n}` : `0${n}` // Adds a 0 if the value is less than 10
}
// Pass in the time in ms
formatTime(time) {
const secs = Math.floor(time / 1000) // divide by 1000 to get secs
const mins = Math.floor(secs / 60) // divide secs by 60 for mins
const hrs = Math.floor(mins / 60) // divide mins by 60 for hrs
// Mod (%) 60 transforms values to 0 to 59 range
// Then pad with 0
const s = padWithZeros(secs % 60)
const m = padWithZeros(mins % 60)
const h = padWithZeros(hrs % 60)
return `${h}:${m}:${s}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment