javascript 检查两个以上的日期范围是否重叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22784883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if more than two date ranges overlap
提问by Niek Nijland
I have multiple date ranges. I want to check if they are overlapping in javascript. When there are only two it is easy, I use:
我有多个日期范围。我想检查它们是否在 javascript 中重叠。当只有两个时很容易,我使用:
if(start_times1 <= end_times2 && end_times1 >= start_times2) {}
But what is the formula when there are more than 2 date ranges?
但是当有超过 2 个日期范围时,公式是什么?
回答by Paul S.
You can use nested for
loops with arguements
您可以使用嵌套for
循环arguements
function dateRangeOverlaps(a_start, a_end, b_start, b_end) {
if (a_start <= b_start && b_start <= a_end) return true; // b starts in a
if (a_start <= b_end && b_end <= a_end) return true; // b ends in a
if (b_start < a_start && a_end < b_end) return true; // a in b
return false;
}
function multipleDateRangeOverlaps() {
var i, j;
if (arguments.length % 2 !== 0)
throw new TypeError('Arguments length must be a multiple of 2');
for (i = 0; i < arguments.length - 2; i += 2) {
for (j = i + 2; j < arguments.length; j += 2) {
if (
dateRangeOverlaps(
arguments[i], arguments[i+1],
arguments[j], arguments[j+1]
)
) return true;
}
}
return false;
}
回答by JueLance
Below code comes from my project, maybe it will help you:
下面的代码来自我的项目,也许对你有帮助:
function dateRangeOverlaps(startDateA, endDateA, startDateB, endDateB) {
if ((endDateA < startDateB) || (startDateA > endDateB)) {
return null
}
var obj = {};
obj.startDate = startDateA <= startDateB ? startDateB : startDateA;
obj.endDate = endDateA <= endDateB ? endDateA : endDateB;
return obj;
}
回答by Vidyesh
Here is refined version of what Paul posted:
这是保罗发布的内容的精炼版本:
- Added filter and null check to allow any number of entries
- Changed the logic so that it can be applied on an array. Eg: [{"from": value, "to": value}]
- Adjusted overlap check to allow times having end and start as same
- 添加过滤器和空检查以允许任意数量的条目
- 更改了逻辑,使其可以应用于数组。例如:[{"from": value, "to": value}]
- 调整重叠检查以允许结束和开始的时间相同
Script:
脚本:
function dateRangeOverlaps(a_start, a_end, b_start, b_end) {
if (a_start < b_start && b_start < a_end) return true; // b starts in a
if (a_start < b_end && b_end < a_end) return true; // b ends in a
if (b_start < a_start && a_end < b_end) return true; // a in b
return false;
}
function multipleDateRangeOverlaps(timeEntries) {
let i = 0, j = 0;
let timeIntervals = timeEntries.filter(entry => entry.from != null && entry.to != null && entry.from.length === 8 && entry.to.length === 8);
if (timeIntervals != null && timeIntervals.length > 1)
for (i = 0; i < timeIntervals.length - 1; i += 1) {
for (j = i + 1; j < timeIntervals.length; j += 1) {
if (
dateRangeOverlaps(
timeIntervals[i].from.getTime(), timeIntervals[i].to.getTime(),
timeIntervals[j].from.getTime(), timeIntervals[j].to.getTime()
)
) return true;
}
}
return false;
}
回答by Pritesh
//storing existing dates for comparison
// 存储现有日期进行比较
public multipleExistingDates=[
{startDate:'02/03/2020 05:00:00',endDate:'02/03/2020 05:30:00'},
{startDate:02/04/2020 05:00:00'',endDate:'02/05/2020 05:00:00'},]
/The date to be compared with existing dates to check if the new date is overlapping with existing dates/
/要与现有日期进行比较以检查新日期是否与现有日期重叠的日期/
public checkOverlappingDsates(startDate:Date, endDate:Date):boolean{
return this.multipleExistingDates.some((elem)=>{
return( !((moment(endDate).diff(moment(elem.startDate))) < 0 ||
(moment(startDate).diff(moment(elem.endDate))) > 0;})
Note: If the date is overlapping, the function return true else false. Also , you would need to install moment for date comparison.
注意:如果日期重叠,则函数返回 true 否则返回 false。此外,您需要安装 moment 以进行日期比较。
回答by EagleV_Attnam
Wouldn't be too hard to do recursively. Make a method overlap
which returns the overlapping daterange for two dates. Then in your hasOverlap(list dates)
method, if the list is two items, it's simple, else, return hasoverlap(overlap(dates[0], dates[1]), rest of list)
递归执行不会太难。制作一个overlap
返回两个日期重叠日期范围的方法。然后在您的hasOverlap(list dates)
方法中,如果列表是两个项目,则很简单,否则,返回hasoverlap(overlap(dates[0], dates[1]), rest of list)