gpt4 book ai didi

Javascript 检查范围是否重叠

转载 作者:行者123 更新时间:2023-11-28 10:49:04 25 4
gpt4 key购买 nike

我有以下 javascript- 对象:

function checkTimeOverlapping() {

var scheduleIntervalContainerWrappers = [];

var scheduleIntervalContainerWrapper1 = {};
scheduleIntervalContainerWrapper1.start = '08:00';
scheduleIntervalContainerWrapper1.end = '10:00';
scheduleIntervalContainerWrappers.push(scheduleIntervalContainerWrapper1);

var scheduleIntervalContainerWrapper2 = {};
scheduleIntervalContainerWrapper2.start = '09:00';
scheduleIntervalContainerWrapper2.end = '14:00';
scheduleIntervalContainerWrappers.push(scheduleIntervalContainerWrapper2);

...

alert(scheduleIntervalContainerWrappers);
}

我将检查scheduleIntervalContainerWrapper是否与开始时间和结束时间重叠。是否有可能在纯js中做到这一点?

最佳答案

带有一些测试用例的提案。转换为分钟。

function overlapping(a, b) {
function getMinutes(s) {
var p = s.split(':').map(Number);
return p[0] * 60 + p[1];
}
return getMinutes(a.end) > getMinutes(b.start) && getMinutes(b.end) > getMinutes(a.start);
}

document.write(overlapping({ start: '00:00', end: '00:00' }, { start: '00:00', end: '00:00' }) + '<br>');
document.write(overlapping({ start: '00:00', end: '02:00' }, { start: '00:00', end: '02:00' }) + '<br>');
document.write(overlapping({ start: '00:00', end: '10:00' }, { start: '02:00', end: '10:00' }) + '<br>');
document.write(overlapping({ start: '20:00', end: '22:00' }, { start: '20:00', end: '21:00' }) + '<br>');
document.write(overlapping({ start: '20:00', end: '21:00' }, { start: '20:00', end: '22:00' }) + '<br>');
document.write(overlapping({ start: '02:00', end: '03:00' }, { start: '01:00', end: '02:30' }) + '<br>');
document.write(overlapping({ start: '00:00', end: '05:00' }, { start: '05:00', end: '10:00' }) + '<br>');
document.write(overlapping({ start: '10:00', end: '15:00' }, { start: '00:00', end: '08:00' }) + '<br>');

关于Javascript 检查范围是否重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com