-
-
Save iShubhamPrakash/37da6ac4a43069e8abec81eb0e610f56 to your computer and use it in GitHub Desktop.
Add two string time values (HH:mm:ss) with javascript
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
/** | |
* Add two string time values (HH:mm:ss) with javascript | |
* | |
* Usage: | |
* > addTimes('04:20:10', '21:15:10'); | |
* > "25:35:20" | |
* > addTimes('04:35:10', '21:35:10'); | |
* > "26:10:20" | |
* > addTimes('30:59', '17:10'); | |
* > "48:09:00" | |
* > addTimes('19:30:00', '00:30:00'); | |
* > "20:00:00" | |
* | |
* @param {String} startTime String time format | |
* @param {String} endTime String time format | |
* @returns {String} | |
*/ | |
function addTimes(startTime, endTime) { | |
var times = [0, 0, 0]; | |
var max = times.length; | |
var a = (startTime || '').split(':'); | |
var b = (endTime || '').split(':'); | |
// normalize time values | |
for (var i = 0; i < max; i++) { | |
a[i] = isNaN(parseInt(a[i])) ? 0 : parseInt(a[i]); | |
b[i] = isNaN(parseInt(b[i])) ? 0 : parseInt(b[i]); | |
} | |
// store time values | |
for (var i = 0; i < max; i++) { | |
times[i] = a[i] + b[i]; | |
} | |
var hours = times[0]; | |
var minutes = times[1]; | |
var seconds = times[2]; | |
if (seconds >= 60) { | |
var m = (seconds / 60) << 0; | |
minutes += m; | |
seconds -= 60 * m; | |
} | |
if (minutes >= 60) { | |
var h = (minutes / 60) << 0; | |
hours += h; | |
minutes -= 60 * h; | |
} | |
return ( | |
Number('0' + hours) + | |
':' + | |
('0' + minutes).slice(-2) + | |
':' + | |
('0' + seconds).slice(-2) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The last change made by me is a fix of a bug mentioned here: https://gist.github.com/joseluisq/dc205abcc9733630639eaf43e267d63f?permalink_comment_id=4343206#gistcomment-4343206