Created
August 18, 2020 18:28
-
-
Save AndreiCalazans/8182e77849b52b476df42f6f24c92a7d to your computer and use it in GitHub Desktop.
From seconds to human readeable hour in TypeScript
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
export function fromSecondsToHHMMSS(value: string) { | |
const sec = parseInt(value, 10); // convert value to number if it's string | |
const hours = Math.floor(sec / 3600); // get hours | |
const minutes = Math.floor((sec - (hours * 3600)) / 60); // get minutes | |
const seconds = sec - (hours * 3600) - (minutes * 60); // get seconds | |
// add 0 if value < 10; Example: 2 => 02 | |
function padWithZero(val: number) { | |
if (val < 10) { | |
return `0${val}`; | |
} | |
return val; | |
} | |
return `${padWithZero(hours)}:${padWithZero(minutes)}:${padWithZero(seconds)}`; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment