Make a function to convert the format mm:ss
to seconds, and one to convert seconds to the format hh:mm:ss
, convert all values to seconds, add them together, and format the result:
function parseMS(s) {
var parts = s.split(':');
return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
}
function formatTwo(n) {
return (n < 10 ? '0' : '') + n.toString();
}
function formatHMS(s) {
var m = Math.floor(s / 60);
s %= 60;
var h = Math.floor(m / 60);
m %= 60;
return formatTwo(h) + ':' + formatTwo(m) + ':' + formatTwo(s);
}
var times = ['14:23', '11:08', '18:59'];
var sum = 0;
for (var i = 0; i < times.length; i++) sum += parseMS(times[i]);
var result = formatHMS(sum);
alert(result);
Demo: http://jsfiddle.net/u6B4g/