0

I've got an Array of time object like which consist of values the user will add. The array could look like this. For simplicity I've just added 01:01:01 to each date value.

let timeObjects = ["01:01:01", "01:01:01", "01:01:01"];

What I want to do is go through the array, and find the total amount of time. So the desired result would be "03:03:03". I've checked for solutions but not been able to solve this yet. Hope someone with experience in date formatting can help me out. FYI I'm using VueJS, Mongo and Express.

egx
  • 389
  • 2
  • 14
  • What would be the sum of `["23:01:01", "02:01:01"]`? – str Nov 20 '20 at 12:42
  • It's not a clock. It's a time counter. So the sum of 23:01:01, 02:01:01 would be 25:02:02. – egx Nov 20 '20 at 12:47
  • Then this should be quite trivial. Loop through the array, `split` by the colon, and sum it up. Which step are you struggling with? – str Nov 20 '20 at 12:48

1 Answers1

0
    let timeObjects = ["03:57:58", "01:35:44", "01:01:01"];

    let aggregate = timeObjects.map(t => t.split(':')).reduce((arr,item,index)=>{
        arr[0]+= parseInt(item[0]);
        arr[1]+= parseInt(item[1]);
        arr[2]+= parseInt(item[2]);
        if(arr[2]>60)
        {
            arr[1]++;
            arr[2]%=60;
        }

        if(arr[1]>60)
        {
            arr[0]++;
            arr[1]%=60;
        }
        return arr;
    },[0,0,0])
    .join(':')
Faran Ali
  • 482
  • 3
  • 12