Last active
January 18, 2020 10:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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