0
votes

Firstly, I have a var that contains a date (just one for the moment):

var disableddates = ["26-05-2018"];

Now I want to check if this date is in a certain date range. The starting date and ending date are coming form a datepicker (with jQuery calendar plugin). These dates are processed like this:

Arrival date:

//Get arrival date from datepicker
function aankomstdatumInput() {
    aankomstDatum = document.getElementById("aankomstdatum").value;
    document.getElementById("aankomst").innerHTML = aankomstDatum;
    var parts = aankomstDatum.split('-');
    aankomstDatumDate = new Date(parts[2],parts2[1]-1,parts2[0]);
}

Departure date:

//Get departure date from datepicker
function vertrekdatumInput() {
    vertrekDatum = document.getElementById("vertrekdatum").value;
    document.getElementById("vertrek").innerHTML = vertrekDatum;
    var parts2 = vertrekDatum.split('-');
    vertrekDatumDate = new Date(parts2[2],parts2[1]-1,parts2[0]);
}

Now I need a function that checks every date in the disableddates array if it's in the range of aankomstDatumDate and vertrekDatumDate.

Since these two dates are formulated in the following way: Sat May 05 2018 00:00:00 GMT+0200 (Romance Daylight Time), I first have to set the disabled date in the same formulation, so I started the function like this:

function checkDisabledDate{
var parts3 = disableddates.split('-');
disabledDatumDate = new Date(parts3[2],parts3[1]-1,parts3[0]);
}

I think that's a good first step, but then I'm stuck at how I can check if disabledDatumDate is between aankomstDatumDate and vertrekDatumDate and then start and else/if depending if the disabled date is in between the arrival date and departure date or not.

1
Are you allowed to use an external library? Moment.js has an isBetween function that makes this task very easy - momentjs.com/docs/#/query/is-between - ThisClark
I would have to ask for it to be allowed and it has to be hosted externally, so, it depends :) - Kevin Verhoeven
The way you're going about it (manual parse) is the best way. You might consider using a library, but if you only have one or two formats to deal with, manual is find. You should declare variables such as aankomstDatum, vertrekDatum, etc. - RobG

1 Answers

0
votes
function checkDisabledDate{

    for(var i = 0; i < disableddates.length; i++){

      var parts3 = disableddates.split('-');
      disabledDatumDate = new Date(parts3[2],parts3[1]-1,parts3[0]);
      var diff1 = vertrekDatumDate - disabledDatumDate;
      var diff2 = vertrekDatumDate - aankomstDatumDate;
      if(diff2 > diff1){

        //disabledDatumDate within date range
        //do something

      }

    }

}