4

I have an array of objects of this type

Obj = { timeEnd:"06:45 AM",
        timeStart:"04:23 AM"
      }

Array is of type [obj1,obj2, ....] of particular day.

This array defines that how much time is spend on activities.

I want to recreate or transform this array into time wise array like

obj = { '4-5 AM' : 37,
        '5-6 AM' : 60,
        '6-7 AM' : 45
      }

I have tried to search could not help. I am a noob.

Please let me know if you need any other info. Ultimate goal is to create a hourwise chart.js graph

Invictus
  • 139
  • 3
  • 16

4 Answers4

3

You could split the 24 h values and count until the target hour is reached and then get the rest of the minutes to add.

function getHourParts(object) {

    function getKey(h) {
       return h + '-' + (h + 1);
    }

    function getTime(s) {
        var t = s.split(':').map(Number);
        return { h: t[0], m: t[1] };
    }

    var result = {},
        start = getTime(object.timeStart),
        end = getTime(object.timeEnd);

    while (start.h < end.h) {
        result[getKey(start.h)] = 60 - start.m;
        ++start.h;
        start.m = 0;
    }
    result[getKey(end.h)] = end.m - start.m;
    return result;
}

console.log(getHourParts({ timeStart: '04:23', timeEnd: '06:45' }));
console.log(getHourParts({ timeStart: '12:03', timeEnd: '12:05' }));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Here is an implementation of how the problem could be resolved. Consider use of Map object makes the benefit of Object distinction (not needed if there is no time overlap) and Array length.

var durations = [{timeStart: "04:23 AM", timeEnd: "06:45 AM", }, {timeStart: "09:23 AM", timeEnd: "11:30 AM", }, {timeStart: "11:09 PM", timeEnd: "01:19 PM", }];

function getValue(time) {
 var [hour, minute, period] = time.replace(':', ' ').split(' ');
 var date = new Date();
 date.setHours((period === 'PM' ? 12 : 0) + +hour, +minute);
 return date;
}

var result = {};

durations.forEach(duration => {
 var time = getValue(duration.timeStart),
  end = getValue(duration.timeEnd);

 for (var hour = time.getHours(); hour <= end.getHours(); hour++) {
  var endMinute = hour === end.getHours() ? end.getMinutes() : 59,
   minute = 0,
   hourSpan = `${hour}-${hour + 1}`;

  result[hourSpan] = result[hourSpan] || new Map;
  while (minute <= endMinute)
   result[hourSpan].set(minute, minute++);
 }
});
Object.keys(result).forEach(span => {
 result[span] = result[span].size;
});
console.clear();
console.log(result)
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
1

You can do this by parsing the time from your input string into a date object and then comparing the date objects:

Working Demo here: http://live.datatables.net/erefom/2/edit#preview

Source here: http://live.datatables.net/erefom/2/edit

Also see this answer: What is the best way to parse a time into a Date object from user input in Javascript?

Mohammad Fareed
  • 1,927
  • 6
  • 26
  • 59
0

The best way to sort any array with respect to time is using the UTC Timestamp value. First convert the stringValue into timestamp and then sort the array according to timestamp values.

Abhishek
  • 3,304
  • 4
  • 30
  • 44
  • How to divide them based on hours ? – Invictus Jun 27 '16 at 16:09
  • I think you can subtract the timestamps to get the actual difference in seconds. – Abhishek Jun 27 '16 at 16:11
  • and you can convert it into hours – Abhishek Jun 27 '16 at 16:12
  • Basically it may sound complicated but its really easy. – Abhishek Jun 27 '16 at 16:12
  • but difference has no am/pm. – Nina Scholz Jun 27 '16 at 17:06
  • to avoid the issue of am/pm I suggested you to use timestamp. It takes care of everything, even the day. – Abhishek Jun 27 '16 at 17:18
  • I have already realized that and converted the dates into 24 hours format. And yes I have got the diffrence in terms of interval but I don't get how the interval can be distributed hourwise. For example if some thing was done in between 12:45 - 13:21. So what I want to do is to save the diffrence in respective 1 hour duration. Like '12-13' will have 15 minutes and '13-14' will have 21. You got me ? @Nina Scholz – Invictus Jun 27 '16 at 18:07