Created
July 26, 2016 08:29
-
-
Save gkio/9222b4347a743fe0eefbb0f22d49c307 to your computer and use it in GitHub Desktop.
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 toSeconds(time_str) { | |
// Extract hours, minutes and seconds | |
var parts = time_str.split(':'); | |
// compute and return total seconds | |
return parts[0] * 3600 + // an hour has 3600 seconds | |
parts[1] * 60 + // a minute has 60 seconds | |
+ | |
parts[2]; // seconds | |
} | |
var a = "10:22:57" | |
var b = "10:30:00" | |
var difference = Math.abs(toSeconds(a) - toSeconds(b)); | |
// format time differnece | |
var result = [ | |
Math.floor(difference / 3600), // an hour has 3600 seconds | |
Math.floor((difference % 3600) / 60), // a minute has 60 seconds | |
difference % 60 | |
]; | |
// 0 padding and concatation | |
result = result.map(function(v) { | |
return v < 10 ? '0' + v : v; | |
}).join(':'); | |
alert(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment