-
-
Save allanfreitas/00cad75a460212ba522b338c05971e6f to your computer and use it in GitHub Desktop.
Sum two times values HH:mm:ss with javascript
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
/** | |
* Sum two times values HH:mm:ss with javascript | |
* Usage: | |
* > addTimes('04:20:10', '21:15:10'); | |
* > "25:35:20" | |
* | |
* @param {string} start | |
* @param {string} end | |
* @returns {String} | |
*/ | |
function addTimes(start, end) { | |
times = []; | |
times1 = start.split(':'); | |
times2 = end.split(':'); | |
for (var i = 0; i < 3; i++) { | |
times1[i] = (isNaN(parseInt(times1[i]))) ? 0 : parseInt(times1[i]) | |
times2[i] = (isNaN(parseInt(times2[i]))) ? 0 : parseInt(times2[i]) | |
times[i] = times1[i] + times2[i]; | |
} | |
var seconds = times[2]; | |
var minutes = times[1]; | |
var hours = times[0]; | |
if (seconds % 60 === 0) { | |
hours += seconds / 60; | |
} | |
if (minutes % 60 === 0) { | |
res = minutes / 60; | |
hours += res; | |
minutes = minutes - (60 * res); | |
} | |
return hours + ':' + minutes + ':' + seconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment