3

I have array-object like 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 can I check if two timings are not overlapping?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
Unnati
  • 348
  • 5
  • 18

3 Answers3

2

Please pass unixtime in array like below example

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 array in time ascending order

var sortedArray = subevent.sort(function(a,b){
    return a.startunix - b.startunix;
});

use sorted array in for loop and store endunix time in one variable lastEndTime and check current iteration start time with lastEndTime variable like 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 display alert or errorflag, if alert display or errorflag is one then something went wrong otherwise everything is ok.

sagar patel
  • 591
  • 5
  • 18
0

Try with new Date().getTime()

In my answer

  1. true means both are same time
  2. Check the status of each object its show the overlap 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)
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • If one object has time of 1:00pm to 2:00pm and another having 1:30pm to 2:30pm, so here the second set of timings are getting overlapped. Hence, how can I check that...first set of timings have already been overlapped? – Unnati Jun 23 '17 at 09:57
0

Try this, it checks if the end date is before start date.

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);
Serge K.
  • 5,303
  • 1
  • 20
  • 27
  • If one object has time of 1:00pm to 2:00pm and another having 1:30pm to 2:30pm, so here the second set of timings are getting overlapped. Hence, how can I check that...first set of timings have already been overlapped? – Unnati Jun 23 '17 at 09:57
  • Apply the same logic, but instead compare each item to others. – Serge K. Jun 23 '17 at 12:22
  • @NathanP. can you please provide some link, demo, jsFiddle or plunker of unnati's above comment ? so i can get better idea ihave same problem and really want to get rid of this situation. – sagar patel Jun 23 '17 at 19:04