Created
February 12, 2012 19:55
-
-
Save vishesh/1810496 to your computer and use it in GitHub Desktop.
Time Difference in format 'HOUR:MONTH'
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
/** | |
* Time Difference | |
* | |
* function : timeDiff(t1, t2) | |
* timeformat : 'HOUR:MINUTES' | |
* | |
*/ | |
function timeInHours(str) | |
{ | |
var sp = str.split(":"); | |
return parseInt(sp[0], 10) + parseFloat(sp[1]/60, 10); | |
} | |
function hoursToString(h) | |
{ | |
var hours = Math.floor(h); | |
var minutes = Math.round((h - hours)*60); | |
return hours + ":" + minutes; | |
} | |
function timeDiff(t1, t2) | |
{ | |
var st1 = timeInHours(t1); | |
var st2 = timeInHours(t2); | |
if (st1 > st2) { | |
return hoursToString(timeDiff(t1, "24:00") + timeDiff("00:00", t2)); | |
} | |
return hoursToString(st2 - st1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment