我有以下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中这样做?
答案 0 :(得分:0)
当然,只需将您的营业时间/分钟转换为当天的分钟数即可。并执行所有必要的检查:
// convert all your value like this
var tab = scheduleIntervalContainerWrapper1.start.split(',');
if(tab[1] < tab[0]){
tab[1]+=24*60;// overlapping on a day
}
var startMinuteOfTheDay = parseInt(tab[0]*60)+parseInt(tab[1]);
关于检查:如果你确定end1总是大于start1而第二个间隔是相同的。
if((start1 > start2 && start1 < end2) || (end1 < end2 && end1 > start2))
答案 1 :(得分:0)
包含一些测试用例的提案。转换为分钟。
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>');
&#13;
答案 2 :(得分:-1)
你必须处理各种重叠案例,即
start1 ... end1
start2 ... end2
start1 ............. end1
start2 ..... end2
start1 ............. end1
start2 ..... end2
start1 ... end1
start2 ........... end2
start1 ... end1
start2 ......... end2
start1 .......... end1
start2 ........... end2
start1 .......... end1
start2 ......... end2
您可以使用if-then-else
记下相应的案例。