Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add two string time values (HH:mm:ss) with javascript
/**
* 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 ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2)
}
@tatyana-ak
Copy link

tatyana-ak commented Oct 18, 2017

There is a bug, if you try something like
addTimes('04:35:10', '21:35:10');
you've got 25:70:20

I fix it like it (from 30 line)

res = (minutes / 60) | 0;
hours += res;
minutes = minutes - (60 * res);

And if you need zero when minutes less then 10, like in my case
('0' + minutes).slice(-2);

@joseluisq
Copy link
Author

joseluisq commented Nov 1, 2017

@grey-mise nice!

@ksantoshkumar540
Copy link

ksantoshkumar540 commented Nov 13, 2017

hey hi dude i used this for my requirement to add 2 time fields contains only hours and minutes i got issue
below is fix for it

let take like addTimes('04:50', '21:20');

for (var i = 0; i < 2; 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) {
            //res = minutes / 60;
            //hours += res;
            //minutes = minutes - (60 * res);
            res = (minutes / 60) | 0;
            hours += res;
            minutes = minutes - (60 * res);

            minutes = minutes < 10 ? "0" + minutes : minutes;

        }


        result = hours + ':' + minutes;

@joseluisq
Copy link
Author

joseluisq commented Dec 8, 2017

I have updated the gist, now it's more concise and covers more use cases.

Examples:

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('00:19:11', '13')
// "13:19:11"

Thanks for your feedback!

@AjeshM
Copy link

AjeshM commented Dec 28, 2017

There is bug in your code try below

addTimes('19:30:00', '00:30:00')
// "19:60:00"

To correct this add greater than equal to both:
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 }
Result:
addTimes('19:30:00', '00:30:00')
// "20:00:00"

@joseluisq
Copy link
Author

joseluisq commented Jan 8, 2018

@AjeshM thanks!

@Gelenkesteffen
Copy link

Gelenkesteffen commented Jan 19, 2018

Hi, for my use case I needed to add support for 24hour calculation so for instance 11:55 + 14:00 = 01:55. Therfore I added the foolowing statement:

if(hours >= 24){
hours = hours %24;
hours = hours < 0 ? 24 + hours : +hours;
}

@joseluisq
Copy link
Author

joseluisq commented Feb 12, 2018

I have published https://github.com/joseluisq/timelite
Feedback welcome!

@TZNaso
Copy link

TZNaso commented Jan 10, 2019

There is a problem when sum more than 99 hours. You can fix just change to -3 (or how many fields you will use, example: if 1000+ hours, you should use -4) in this line: return ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2)

@myas92
Copy link

myas92 commented Dec 26, 2019

I think for this return ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2) you should write this :
hours = (hours < 10) ? 0${hours} : hours
minutes = (minutes < 10) ? 0${minutes} : minutes
seconds = (seconds < 10) ? 0${seconds} : seconds
return ${hours}:${minutes}:${seconds}

because for sum more than 99 hours, result is incorrect

@ganmor
Copy link

ganmor commented Aug 1, 2020

You are missing radis parameter in parse int

If radix is undefined or 0 (or absent), JavaScript assumes the following: If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed. If the input string begins with "0", radix is eight (octal) or 10 (decimal).

@vanpariyar
Copy link

vanpariyar commented Jan 7, 2021

Just implement your code in this tools https://vanpariyar.github.io/useful-tools

Thank you so much ...

@olmos31
Copy link

olmos31 commented Apr 16, 2021

Thank you!

@trynx
Copy link

trynx commented Dec 27, 2021

If there is a need for longer hours, not only 2 digits like in my case, you can change in the return of the hours from split(-2) to split(-3)

return ('0' + hours).slice(-3) + ':' + ('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