How to check time coincidence in jquery or javascript
I have an array object as shown below in 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)
},
.
.
.
]
How to check if two timings overlap?
+3
source to share
3 answers
Please pass unixtime in an array like below.
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
}
]
then sorting the array in ascending order
var sortedArray = subevent.sort(function(a,b){
return a.startunix - b.startunix;
});
use sorted array for loop and store endunix time in one variable lastEndTime
and check current start time of iteration with variable lastEndTime
as shown below code
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);
there is also a warning message or error flag, if the warning or error flags, then something went wrong, otherwise everything will be fine.
+1
source to share
Try with new Date().getTime()
In my answer
-
true
means both are the same - Check
status
each object for its display overlay or not
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)
0
source to share
Try this, it checks, until the end date is before the start.
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);
0
source to share