Skip to content

Instantly share code, notes, and snippets.

@AndreiCalazans
Created August 18, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreiCalazans/8182e77849b52b476df42f6f24c92a7d to your computer and use it in GitHub Desktop.
Save AndreiCalazans/8182e77849b52b476df42f6f24c92a7d to your computer and use it in GitHub Desktop.
From seconds to human readeable hour in TypeScript
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