Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Created July 31, 2014 23:22
Show Gist options
  • Save leocaseiro/6f70141649c2953cb8d2 to your computer and use it in GitHub Desktop.
Save leocaseiro/6f70141649c2953cb8d2 to your computer and use it in GitHub Desktop.
Add Hours to Javascript Time
function addHourTo(start, duration) {
start = start.split(':');
duration = duration.split(':');
totalHours = parseInt(start[0], 10) + parseInt(duration[0], 10);
totalMinutes = parseInt(start[1], 10) + parseInt(duration[1], 10);
if (totalMinutes >= 60) {
totalMinutes -= 60;
totalHours += 1;
}
result = zeroOnLeft(totalHours) + ":" + zeroOnLeft(totalMinutes);
return result;
}
function zeroOnLeft( n ){
return ( n < 10 ? "0" + n : n);
}
//Usage:
var finish_time = addHourTo('15:45','00:30');
//finish_time = '16:15'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment