Last active
April 11, 2018 17:33
-
-
Save mukhortov/91ce11f5b63e84d1948ced2aab48cc70 to your computer and use it in GitHub Desktop.
Convert seconds to HH:MM:SS
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 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