2

I've got the following object in an array[0]:

var arr[0]=
[
 {
   "startTime": "1300",
    "endTime": "1700",
    "eventName": "Sleep",
     "end_datetime": "20180510M0100", 
     "start_datetime": "20180509M2300", 
 },
 {
    "startTime": "0800",
    "endTime": "1200", 
    "eventName": "Breakfast",
     "end_datetime": "20180507M1200", 
     "start_datetime": "20180507M0800", 
 },
 {
   "startTime": "1300",
    "endTime": "1400",
    "eventName": "Lesson",
    "end_datetime": "20180507M1400", 
     "start_datetime": "20180507M1300", 
 },
 {
    "startTime": "1300",
    "endTime": "1700",
    "eventName": "Ski",
    "end_datetime": "20180511M170000", 
    "start_datetime": "20180511M130000",
 } 
]

end_datatime is in format yyyymmddMhhmmss(first 4 digits are year, follow by months, days, separator 'M', hours, minutes, and seconds.

I would like to calculate total duration of all the events? (Total of 11 hours which is 2 hours in sleep + 4 hours in breakfast + 1 hour in lesson + 4 hours in ski)

user21
  • 1,261
  • 5
  • 20
  • 41
  • So you want the overall start time and end time for all the events (such as 1700-800? – Andrew Li May 08 '18 at 02:37
  • I would like to output the total time spent in all the events. (in this case is total of 11 hours which is 2 hours in sleep + 4 hours in breakfast + 1 hour in lesson + 4 hours in ski) – user21 May 08 '18 at 03:16

2 Answers2

1

You need an accumulator to keep track of the time thus far. I am assuming the numbers refer to minutes or something. If it is actually time (as in 1700 = 5PM), then you'd need to do more logic obviously (because the event could start at 1159 and end at 0100 and in such a case, startTime - endTime will not work), but from your attempt it seems like you are just looking for the diffeerence between the two numbers. Here is what I got:

arr = [
{
   "startTime": "1300",
    "endTime": "1700",
    "eventName": "Tea" 
 },
 {
    "startTime": "0800",
    "endTime": "1200", 
    "eventName": "Breakfast" 
 },
 {
   "startTime": "1300",
    "endTime": "1400",
    "eventName": "Lesson" 
 },
 {
    "startTime": "1300",
    "endTime": "1700",
    "eventName": "Ski"
 } 
]

arr.reduce((acc, val) => {
  return acc + (parseInt(val.endTime) - parseInt(val.startTime))
}, 0)

// returns 1300
Abid Hasan
  • 648
  • 4
  • 10
  • Hi the numbers refer to hhmm (first 2 digits is hours, and last 2 digits is minutes). How do I get the total time difference. can I use momentJS diff inside the reduce function? – user21 May 08 '18 at 02:55
  • Absolutely. You can use any date/time library or the built in Date object to do more complex time difference calculations. It really depends on how complicated your times can get – Abid Hasan May 08 '18 at 03:14
  • Okay. Sorry I just modify the question taking into consideration of the event could start at 1100 and end at 0100 next day. – user21 May 08 '18 at 03:18
1

Here's one possible implementation, taking into consideration times spanning multiple days:

const input = [{
    "startTime": "1300",
    "endTime": "1700",
    "eventName": "Sleep",
    "end_datetime": "20180510M0100",
    "start_datetime": "20180509M2300",
  },
  {
    "startTime": "0800",
    "endTime": "1200",
    "eventName": "Breakfast",
    "end_datetime": "20180507M1200",
    "start_datetime": "20180507M0800",
  },
  {
    "startTime": "1300",
    "endTime": "1400",
    "eventName": "Lesson",
    "end_datetime": "20180507M1400",
    "start_datetime": "20180507M1300",
  },
  {
    "startTime": "1300",
    "endTime": "1700",
    "eventName": "Ski",
    "end_datetime": "20180511M170000",
    "start_datetime": "20180511M130000",
  }
];

function getDiff(datestr1, datestr2) {
  const m1 = moment(datestr1, 'YYYYMMDD*hhmm');
  const m2 = moment(datestr2, 'YYYYMMDD*hhmm');
  const minuteDifference = m2.diff(m1, 'minutes');
  return minuteDifference;
}
const totalMinutes = input.reduce(
  (accum, {
    end_datetime,
    start_datetime
  }) => accum + getDiff(start_datetime, end_datetime),
  0
);
const totalHours = totalMinutes / 60;
console.log(totalMinutes + ' minutes = ' + totalHours + ' hours');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320