Skip to content

Instantly share code, notes, and snippets.

@johnatandias
Last active November 30, 2020 19:40
Show Gist options
  • Save johnatandias/9ce36e93fda32948df57291c4b32f3e8 to your computer and use it in GitHub Desktop.
Save johnatandias/9ce36e93fda32948df57291c4b32f3e8 to your computer and use it in GitHub Desktop.
Convert Milliseconds to Hours
function msToTime(duration = 0) {
const seconds = `0${Math.floor((duration / 1000) % 60)}`.slice(-2)
const minutes = `0${Math.floor((duration / (1000 * 60)) % 60)}`.slice(-2)
const hours = `0${Math.floor((duration / (1000 * 60 * 60)) % 24)}`.slice(-2)
return `${hours}:${minutes}:${seconds}`;
}
console.log(msToTime(97739)) // 00:01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment