使用时刻检查时间是否重叠? [英] Check if times overlap using moment?
问题描述
我的数组 [start_time,end_time]
时间范围如下:
I have an array of [start_time, end_time]
time ranges like so:
let timeSegments = [];
timeSegments.push(["02:00", "07:00"])
timeSegments.push(["03:00", "04:00"])
这些时间段重叠,因为 2AM - 7AM
包括凌晨3点 - 凌晨4点
These time segments overlap, since 2AM - 7AM
includes 3AM - 4AM
同样:
let timeSegments = [];
timeSegments.push(["14:00", "18:00"])
timeSegments.push(["15:00", "19:00"])
2PM
到 6PM
与 3PM
重叠至 7PM
。
I我正在使用momentjs库,并想知道一种方法来确定我的timeSegments数组是否包含任何重叠的timeSegments? timeSegments数组最多可包含10个 [start_time,end_time]
对。谢谢!
I'm using the momentjs library, and would like to know a way to determine if my timeSegments array contains any timeSegments that overlap? The timeSegments array can contain at most 10 [start_time, end_time]
pairs. Thanks!
我只想知道是否有任何段重叠(真/假),我不需要知道哪些段重叠等。
I'd just like to know if any segments overlap (true/false), I don't need to know which of the segments overlap etc.
推荐答案
您可以通过 start_time <对
timeSegments
进行排序/ code>(使用 Array.prototype.sort
)并遍历排序列表并检查 end_time
大于下一个的 start_time
。
You can sort timeSegments
by the start_time
(using Array.prototype.sort
) and iterate through the sorted list and check if the end_time
of the current timeSegment is greater than the start_time
of the next one.
如果发生这种情况,则会有重叠。
If that happens, there is overlap.
这篇关于使用时刻检查时间是否重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!