0

I am trying to find the best approach to comparing date/times using Javascript in order to prevent double booking on a SharePoint calendar. So I load an array with items that contain each event, including their start date/time and end date/time. I want to compare the start date/time and end date/time against the start/end date/times in the object, but I am not sure how to ensure that dates will not lapse.

like:

//date that is created from user controls
var startDate = new Date(startDat + 'T' + startHour + ':' + startMin + ':00');
var endDate = new Date(endDat+ 'T' + endHour+ ':' + endMin+ ':00');
for ( var i = 0; i < allEvents.length; i++ ) {
   var thisEvent = allevents[i];
   //having trouble with the compare

   //i have tried silly ifs like
   if (thisEvent.startDate >= startDate && thisEvent.endDate <= endDate) { 
      // this seems like I am going down the wrong path for sure
   }
}

I then tried breaking apart the loaded object into seperate values (int) for each component of the date

var thisObj = { startMonth: returnMonth(startDate), startDay: returnDay(startDate), etc

but I am not sure this isn't just another silly approach and there is another that just makes more sense as I am just learning this.

Justin
  • 4,461
  • 22
  • 87
  • 152
  • 1
    So your question is basically about how to compare two dates? You may want to look at this answer: http://stackoverflow.com/a/497790/1134119 – AymKdn Mar 06 '14 at 09:55

1 Answers1

1

I have a similar requirement in progress but chose to solve it at the booking stage, with jQuery/SPServices.

The code is still in build (ie not finished) but the method may help.

I attach an event handler to a column, then on selection, fetch all the dates booked in the same list to an array, then display that array on a rolling 12 month cal, as below.

enter image description here

I'm not checking to ensure a new booking doesn't overlap but a quick scan through the array on Pre-Save would provide a strict Go/No Go option for me. Relies on client side JS though, so not going to work in a datasheet or web services context.

Paul Leigh
  • 1,231
  • 7
  • 7