Created
July 31, 2014 23:22
-
-
Save leocaseiro/6f70141649c2953cb8d2 to your computer and use it in GitHub Desktop.
Add Hours to Javascript Time
This file contains hidden or 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 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