Skip to content

Instantly share code, notes, and snippets.

@RedStone576
Last active March 19, 2022 05:10
Show Gist options
  • Save RedStone576/5587c3cf8f31622577d9889b5ada6b65 to your computer and use it in GitHub Desktop.
Save RedStone576/5587c3cf8f31622577d9889b5ada6b65 to your computer and use it in GitHub Desktop.
convert milliseconds to hours:minute:seconds.milliseconds
//https://stackoverflow.com/questions/19700283/how-to-convert-time-in-milliseconds-to-hours-min-sec-format-in-javascript
function time(duration) {
let answerToLife = parseInt(duration % 1000)
let seconds = Math.floor((duration / 1000) % 60)
let minutes = Math.floor((duration / (1000 * 60)) % 60)
let hours = Math.floor((duration / (1000 * 60 * 60)) % 24)
hours = hours < 10 ? "0" + hours : hours
minutes = minutes < 10 ? "0" + minutes : minutes
seconds = seconds < 10 ? "0" + seconds : seconds
answerToLife = duration > 999 ? (seconds + ".") + answerToLife : answerToLife
answerToLife = duration > 59999 ? (minutes + ":") + answerToLife : answerToLife
answerToLife = duration > 3599999 ? (hours + ":") + answerToLife : answerToLife
return answerToLife
}
time(69420727) //"19:17:00.727"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment