我在jquery
中有如下所示的数组对象var subevent = [
{
start: Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time),
end: Fri Jun 23 2017 14:15:00 GMT+0530 (India Standard Time)
},
{
start: Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time),
end: Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time)
},
{
start: Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time),
end: Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)
},
.
.
.
]
如何检查两个时间是否重叠?
答案 0 :(得分:2)
请在数组中传递unixtime,如下例
var subevent = [
{
start: Mon Jun 26 2017 10:15:00 GMT+0530 (India Standard Time),
startunix: 1498452300000,
end: Mon Jun 26 2017 11:15:00 GMT+0530 (India Standard Time),
endunix: 1498455900000
},
{
start: Mon Jun 26 2017 11:15:00 GMT+0530 (India Standard Time),
startunix: 1498455900000,
end: Mon Jun 26 2017 12:15:00 GMT+0530 (India Standard Time),
endunix: 1498459500000
}
]
然后按时间升序排序数组
var sortedArray = subevent.sort(function(a,b){
return a.startunix - b.startunix;
});
在for循环中使用有序数组并将endunix时间存储在一个变量lastEndTime
中,并使用lastEndTime
变量检查当前迭代开始时间,如下面的代码
var errorFlag = 0;
var lastEndTime;
for(var i=0; i<sortedArray.length; i++) {
var currentStartTime;
if( sortedArray[i].endunix <= sortedArray[i].startunix ){
alert('time slot conflict')
errorFlag = 1;
break;
}
if( !lastEndTime ) {
lastEndTime = sortedArray[i].endunix;
//console.log(" i where last end time not exists = "+i+" lastEndTime "+lastEndTime);
} else {
currentStartTime = sortedArray[i].startunix;
//console.log(" i where last end time exists = "+i+" currentStartTime "+currentStartTime+" lastEndTime "+lastEndTime );
if ( currentStartTime < lastEndTime ) {
alert('time overlapping')
errorFlag = 1;
break;
}
lastEndTime = sortedArray[i].endunix;
}
}
console.log(errorFlag);
还有显示警告或错误标志,如果警告显示或错误标志是一个则出现问题,否则一切正常。
答案 1 :(得分:0)
尝试使用new Date().getTime()
在我的回答中
true
表示两者同时发生status
是否显示重叠 li>
醇>
var subevent = [{ start: "Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)", end: "Fri Jun 23 2017 14:15:00 GMT+0530 (India Standard Time)"},{ start: "Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)", end: "Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time)"}, { start: "Fri Jun 23 2017 16:15:00 GMT+0530 (India Standard Time)", end: "Fri Jun 23 2017 15:15:00 GMT+0530 (India Standard Time)"},]
subevent.forEach(function(a) {
a.status = new Date(a.start).getTime() == new Date(a.end).getTime();
})
console.log(subevent)
答案 2 :(得分:0)
试试这个,它检查结束日期是否在开始日期之前。
var subevent = [{
start: 'Fri Jun 23 2017 15:15:00 GMT+0530(India Standard Time)',
end: 'Fri Jun 23 2017 14:15:00 GMT+0530(India Standard Time)'
},
{
start: 'Fri Jun 23 2017 15:15:00 GMT+0530(India Standard Time)',
end: 'Fri Jun 23 2017 16:15:00 GMT+0530(India Standard Time)'
},
{
start: 'Fri Jun 23 2017 16:15:00 GMT+0530(India Standard Time)',
end: 'Fri Jun 23 2017 15:15:00 GMT+0530(India Standard Time)'
}
];
for (let event of subevent) {
let startTime = new Date(event.start).getTime(),
endTime = new Date(event.end).getTime();
event.isOverlapping = endTime <= startTime;
}
console.log(subevent);