Skip to content

Instantly share code, notes, and snippets.

@flleeppyy
Last active February 14, 2021 14:43
Show Gist options
  • Save flleeppyy/a8e4329825c1f66f18dfb3c8f02ec8ac to your computer and use it in GitHub Desktop.
Save flleeppyy/a8e4329825c1f66f18dfb3c8f02ec8ac to your computer and use it in GitHub Desktop.
// Input a number and it will format like so, trimming where neccesary
// HH:MM:SS
// 1:12
// 12:42
// 1:34:15
// 10:00:01
// etc
function convertHMS(time) {
let hours = Math.floor(time / 3600);
time %= 3600;
let minutes = Math.floor(time / 60);
let seconds = Math.floor(time % 60);
let final = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
(final.startsWith('00:0')) ? final = final.slice(4) :
(final.startsWith('00:')) ? final = final.slice(3) :
(final.startsWith('0')) ? final = final.slice(1) : null;
return final
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment