在处理自定义日历时,我不知道如何找到与任何其他时间段重叠的时间段。
时间段从 0 到 720(上午 9 点到晚上 9 点,每个像素代表一分钟)。
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
];
每个时隙为1 小时,例如 9 到 10、10 到 11、11 到 12 等。
在上面的示例中,三个事件(id:1,2,3)在9-10 开始时间重叠9:00:9:30和9:20。其他重叠的事件是 int time slot of 6to 7(id: 5, 6) with 6and 6:20 start times。id 的事件在to4的时间段内没有任何重叠事件。121
我正在寻找一种方法来获取所有重叠的事件 ID 以及特定时间段中的事件数,这是预期的输出:
[
{id:1, eventCount: 3},
{id:2, eventCount: 3},
{id:3, eventCount: 3},
{id:5, eventCount: 2},
{id:6, eventCount: 2}
]
对于 id (1 到 3),有3时间槽 to 的事件和时间9槽10to的2事件。67
我创建了这个公式来将时间数转换为实际时间:
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();
这是我到目前为止所拥有的:
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
}
}