I have an array consisting of time durations like ['00:30', '01:30', '03:00', '04:30'] and I am trying to add the array elements to get total time duration. I am trying the below code to do this job but I am getting a irrelevant output like 00000.5010.503040.5:0. Have anyone tried this kind of sum earlier?
function calc_tot_dur() {
var total_durs = ['00:30', '01:30', '03:00', '04:30'];
var dim = '00:00';
jQuery.each(total_durs, function(index, value) {
//console.log(value);
dim_split = dim.split(":");
hs = dim_split[0];
ms = dim_split[1];
value_split = value.split(":");
v_hs = value_split[0];
v_ms = value_split[1];
console.log(hs + v_hs);
dim = (hs + v_hs) + ':' + (ms + v_ms);
// console.log(dim);
ms_hs = (ms + v_ms) / 60;
if (ms_hs > 0) {
dim = (hs + v_hs + ms_hs) + ':' + (00);
} else {
dim = (hs + v_hs) + ':' + (ms + v_ms);
}
});
alert(dim);
}
dimvariable for every iteration of the loop.