While working on custom calendar, I can't figure out how to find time slots that overlaps any other time slot.
Time slots start from 0 to 720 (9am to 9pm with each pixel representing a minute).
var events = [
{id : 1, start : 0, end : 40}, // an event from 9:00am to 9:40am
{id : 2, start : 30, end : 150}, // an event from 9:30am to 11:30am
{id : 3, start : 20, end : 180}, // an event from 9:20am to 12:00am
{id : 4, start : 200, end : 230}, // an event from 12:20pm to 12:30pm
{id : 5, start : 540, end : 600}, // an event from 6pm to 7pm
{id : 6, start : 560, end : 620} // an event from 6:20pm to 7:20pm
];
Each time slots is of one hour, for example 9 to 10, 10 to 11, 11 to 12 and so on.
In the above example, three events (id: 1,2,3) are overlapping for the 9-10
start time: 9:00
, 9:30
and 9:20
. And other events overlapping are int time slot of 6
to 7
(id: 5, 6) with 6
and 6:20
start times. The event with id 4
doesn't have any overlapping events in the time slot of 12
to 1
.
I am looking for a way to get all overlapping event ids as well as number of events in a particular time slot, this is expected output:
[
{id:1, eventCount: 3},
{id:2, eventCount: 3},
{id:3, eventCount: 3},
{id:5, eventCount: 2},
{id:6, eventCount: 2}
]
For ids (1 to 3), there are 3
events for time slot 9
to 10
and 2
events for time slot 6
to 7
.
I have created this formula to convert time number to actual time:
var start_time = new Date(0, 0, 0, Math.abs(events[i].start / 60) + 9, Math.abs(events[i].start % 60)).toLocaleTimeString(),
var end_time = new Date(0, 0, 0, Math.abs(events[i].end / 60) + 9, Math.abs(events[i].end % 60)).toLocaleTimeString();
This is what I have so far:
function getOverlaps(events) {
// sort events
events.sort(function(a,b){return a.start - b.start;});
for (var i = 0, l = events.length; i < l; i++) {
// cant figure out what should be next
}
}