Skip to content

Instantly share code, notes, and snippets.

@murillomarigo
Created June 4, 2013 17:47
Show Gist options
  • Save murillomarigo/5707964 to your computer and use it in GitHub Desktop.
Save murillomarigo/5707964 to your computer and use it in GitHub Desktop.
Javascript Date Utils -secondsToHms -hmsToSeconds
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