我有多个日期范围。我想检查它们是否在javascript中重叠。当只有两个很容易时,我使用:
if(start_times1 <= end_times2 && end_times1 >= start_times2) {}
但是当有超过2个日期范围时,公式是什么?
答案 0 :(得分:16)
您可以使用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;
}
答案 1 :(得分:3)
以下是保罗发布的内容的精炼版本:
脚本:
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;
}
答案 2 :(得分:2)
以下代码来自我的项目,也许它会对您有所帮助:
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;
}
答案 3 :(得分:0)
递归不会太难。创建一个方法overlap
,返回两个日期的重叠日期范围。然后在您的hasOverlap(list dates)
方法中,如果列表是两个项目,则很简单,否则返回hasoverlap(overlap(dates[0], dates[1]), rest of list)
答案 4 :(得分:0)
//存储现有日期以进行比较
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'},]
/ 要与现有日期进行比较的日期,以检查新日期是否与现有日期重叠 /
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;})
注意:如果日期重叠,则函数返回true,否则返回false。另外,您还需要安装片刻以进行日期比较。
答案 5 :(得分:0)
为什么我们不使用 moment 和 moment-range,不是所有浏览器都支持吗? ?
window['moment-range'].extendMoment(moment);
const events1 = [{
"Date": "05/15/2021",
"EndTime": "17:00",
"StartTime": "16:00"
},
{
"Date": "05/15/2021",
"EndTime": "18:00",
"StartTime": "17:00"
},
{
"Date": "05/15/2021",
"EndTime": "18:45",
"StartTime": "17:45"
}
];
const events2 = [{
"Date": "05/15/2021",
"EndTime": "17:00",
"StartTime": "16:00"
},
{
"Date": "05/15/2021",
"EndTime": "18:00",
"StartTime": "17:00"
},
{
"Date": "05/15/2021",
"EndTime": "19:45",
"StartTime": "18:45"
}
];
function checkOverlap(timeSegments) {
var overlap = timeSegments
.map(r =>
timeSegments.filter(q => q != r).map(q =>
moment.range(
moment(q.Date + " " + q.StartTime),
moment(q.Date + " " + q.EndTime)
).overlaps(
moment.range(
moment(r.Date + " " + r.StartTime),
moment(r.Date + " " + r.EndTime)
)
)
)
);
console.log(overlap.map(x => x.includes(true)).includes(true));
}
checkOverlap(events1);
checkOverlap(events2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/4.0.2/moment-range.js"></script>