Created
June 4, 2013 17:47
-
-
Save murillomarigo/5707964 to your computer and use it in GitHub Desktop.
Javascript Date Utils
-secondsToHms
-hmsToSeconds
This file contains 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 secondsToHms(d) { | |
d = Number(d); | |
var h = Math.floor(d / 3600); | |
var m = Math.floor(d % 3600 / 60); | |
var s = Math.floor(d % 3600 % 60); | |
return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s); | |
} | |
function hmsToSeconds(str) { | |
var p = str.split(':'), | |
s = 0, m = 1; | |
while (p.length > 0) { | |
s += m * parseInt(p.pop()); | |
m *= 60; | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment