Skip to content

Instantly share code, notes, and snippets.

@mukhortov
Last active April 11, 2018 17:33
Show Gist options
  • Save mukhortov/91ce11f5b63e84d1948ced2aab48cc70 to your computer and use it in GitHub Desktop.
Save mukhortov/91ce11f5b63e84d1948ced2aab48cc70 to your computer and use it in GitHub Desktop.
Convert seconds to HH:MM:SS
const timerFormatter = (secs: number): string => {
const secondsInDay = 60 * 60 * 24
const secondsInHour = 60 * 60
const secondsInMinute = 60
const hoursInDay = 24
const days = Math.floor(secs / secondsInDay)
const hours = Math.floor((secs / secondsInHour) % hoursInDay).toString().padStart(2, 0)
const minutes = Math.floor((secs / secondsInMinute) % secondsInMinute).toString().padStart(2, 0)
const seconds = (secs % secondsInMinute).toString().padStart(2, 0)
let formatedDays = ''
if (days > 0) {
const plural = days === 1 ? '' : 's'
formatedDays = `${days} day${plural} `
}
return `${formatedDays}${hours}:${minutes}:${seconds}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment