I have an array of time ranges with start & end values.
var timeRanges = [{
start: 120,
end: 140
},{
start: 180,
end: 220
},{
start: 250,
end: 300
}]
Need to check whether the selected range overlap the time range or not. And also the selected range should be in between the timeRange intervals. (Ex: 140-180, 220-250)
var selected = {
start: 150,
end: 170
}
Time interval B 'overlaps' A if:
B starts after A starts but before A finishes.B starts before A starts and finishes after A starts.So you can write a function which decides exactly that.
function areOverlapping(A, B) {
if(B.start < A.start) {
return B.finish > A.start;
}
else {
return B.start < A.finish;
}
}
const overlaps = timeRanges.some( range =>
(range.start < selected.start && range.end > selected.start) ||
(range.start < selected.end && range.end > selected.end) ||
(range.start > selected.start && range.end < selected.end)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With