Skip to content

Instantly share code, notes, and snippets.

@vishesh
Created February 12, 2012 19:55
Show Gist options
  • Save vishesh/1810496 to your computer and use it in GitHub Desktop.
Save vishesh/1810496 to your computer and use it in GitHub Desktop.
Time Difference in format 'HOUR:MONTH'
/**
* 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