1

I have two arrays of different time string.

startingTime: string["09:00:00", "5:50:00", "6:30:00"];
duration: string["0:15:00", "01:00:00", "01:00:00"];

I want to sum these arrays in final like

endingTime: string ["09:15:00", "6:50:00", "7:30:00"]

I'm building Single page application using angular 7,trying to use momentjs but not sure how to implement it. I would appreciate any help.

Srinivasan Sekar
  • 2,049
  • 13
  • 22
Galla87
  • 11
  • 3
  • 1
    Hi, what have you tried so far? Please share some code, eventually a small repro on stackblitz. – maxime1992 May 31 '19 at 09:48
  • 1
    Possible duplicate of [Sum of Time using javascript](https://stackoverflow.com/questions/26056434/sum-of-time-using-javascript) – wentjun May 31 '19 at 09:51

3 Answers3

2
addTimes(start, end) {
  var a = start.split(":");
  var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
  var b = end.split(":");
  var seconds2 = (+b[0]) * 60 * 60 + (+b[1]) * 60 + (+b[2]);

  var date = new Date(1970, 0, 1);
  date.setSeconds(seconds + seconds2);
  var c = date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
  return c;
}

addDurationToHours() {
  let hours = ["09:00:00", "5:50:00", "6:30:00"]; 
  let duration = ["0:15:00", "01:00:00", "01:00:00"]; 

  let newArray = [];
  hours.forEach( ( hour, index ) => {
    newArray.push( this.addTimes(hour, duration[index]))
  })
  console.log ( newArray )
  return newArray;
}

I referred Adding HH:MM:SS strings in Javascript for addTimes() method

  • Its returning me an error-> Cannot read property 'push' of undefined. Thanks for help. – Galla87 May 31 '19 at 11:57
  • I've created let newArray = [], so it should not throw push of undefined. I tested the code in https://stackblitz.com. cud you share your code? – Godwin Stanislaus May 31 '19 at 12:05
  • oo I'm sorry I created new Array above and ofc it returned me an error. This works as a charm. Thanks a bunch!!! – Galla87 May 31 '19 at 12:15
1

Try like this:

let seta = ["09:00:00", "5:50:00", "6:30:00"];
let setb = ["0:15:00", "01:00:00", "01:00:00"]

let totalArray = []

seta.forEach((time,index) => {
  totalArray.push(this.addTimes([time,setb[index]))
});


addTimes(times) {

const z = (n) => (n < 10 ? '0' : '') + n;

let hour = 0
let minute = 0
let second = 0
for (const time of times) {
    const splited = time.split(':');
    hour += parseInt(splited[0]);
    minute += parseInt(splited[1])
    second += parseInt(splited[2])
}
const seconds = second % 60
const minutes = parseInt(minute % 60) + parseInt(second / 60)
const hours = hour + parseInt(minute / 60)

return z(hours) + ':' + z(minutes) + ':' + z(seconds)
}
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
  • const minutes = parseInt(minute % 60) + parseInt(second / 60) is giving an error, Argument of type 'number' is not assignable to parameter of type 'string'. Thx for trying to help. – Galla87 May 31 '19 at 11:36
0

@Godwin Stanislaus Your code works fine for me except for an error, str.split() is not a function. However, appending +' ' to string solved that issue.

Below code returns sum of multiple time strings(HH:MM:SS)

  var timedata=['00:00:08', '00:22:78', '02:22:01', '00:15:07'];
  var total=this.sumTime(timedata);
  console.log('total time spent', total);


  sumTime(arr) {

  var seconds:number=0;
  var date = new Date(1970,0,1);
  arr.forEach(element => { 
    var a = (element+'').split(":");
    var sec = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 
    seconds=seconds+sec;
  });
  
  
  date.setSeconds(seconds);

  var c = date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
  return c;
}
Pallavi
  • 496
  • 5
  • 12