While working with a custom calendar, I can't figure out how to find time intervals that span any other time interval.
Time intervals start from 0 to 720 (from 9 a.m. to 9 p.m., each pixel is 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 interval is one hour, for example, from 9 to 10, from 10 to 11, from 11 to 12, etc.
In the above example, the three events (id: 1,2,3) overlap for the start times of 9-10
: 9:00
, 9:30
and 9:20
. And other overlapping events are time intervals from 6
to 7
(id: 5, 6) with launch times of 6
and 6:20
. An event with id 4
has no overlapping events in the time interval from 12
to 1
.
I am looking for a way to get all overlapping event IDs, as well as the number of events in a particular time interval, this is the expected result:
[ {id:1, eventCount: 3}, {id:2, eventCount: 3}, {id:3, eventCount: 3}, {id:5, eventCount: 2}, {id:6, eventCount: 2} ]
For identifiers (1 to 3), there are events 3
for time intervals 9
to 10
and 2
for time interval 6
- 7
.
I created this formula to convert a temporary number to an 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) {
DEMO if you need to.