Skip to content

Instantly share code, notes, and snippets.

@ndabAP
Last active January 18, 2020 10:47
Show Gist options
  • Save ndabAP/6258c778edfd4f91895af66b7dc29882 to your computer and use it in GitHub Desktop.
Save ndabAP/6258c778edfd4f91895af66b7dc29882 to your computer and use it in GitHub Desktop.
Convert seconds into format HH:MM:SS in JavaScript. Works for negative values as well
function nSecToTime(s) {
let seconds = s
s = Math.abs(s)
let t = [0, 0, 0]
let r = s % 3600
t[0] = Math.floor(s / 3600)
t[1] = Math.floor(r / 60)
r = r % 60
t[2] = r
return (seconds < 0 ? "-" : "") + (t[0] < 10 ? "0" : "") + t[0]+":"+(t[1]<10?"0"+t[1]:t[1])+":"+(t[2]<10?"0"+t[2]:t[2])
}
let s = 60 * 60 + 5
console.log(nSecToTime(-s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment