Jquery UI datepicker - disconnect date ranges

I'm trying to make something work - I get the message "Uncaught TypeError: cannot read property" 0 "from undefined" with the following, and I can’t understand why!

I am trying to use the jQuery UI datepicker on a holiday cottage site to indicate availability and season (low, high, etc.). I have a function as a datePicker event to check if there is a reservation for a date, and if not, then I go out to check what season we are in. (Off-peak seasonal orders can be done on Monday or Friday. At peak season, this is only on Fridays.)

I use cms to create some arrays of dates (through loops) that can then be executed when creating a calendar, so javascript is a bit detailed.

Arrays are as follows:

<script> 
//Peak Season 1 2011
var ps1 = new Date('June 17, 2011');
var pe1 = new Date('September 2, 2011');
//Peak Season 2 2011
var ps2 = new Date('December 19, 2011');
var pe2 = new Date('January 6, 2012');
// season start and end date arrays
var peakStart = new Array(ps1,ps2);
var peakEnd = new Array(pe1,pe2);

// Bookings
//Mr &amp; Mrs Smith 
var cbs1 = new Date('May 27, 2011');
var cbe1 = new Date('June 5, 2011');
//Mr &amp; Mrs Jones 
var cbs2 = new Date('September 1, 2011');
var cbe2 = new Date('September 18, 2011');
var cottageStart = new Array(cbs1,cbs2);
var cottageEnd = new Array(cbe1,cbe2);

// last date of season - don't book past here
var lastDate = '01/06/2012';
</script>

I have the following function that I call from the beforeShowDate event to check the calendar date on the reservation array:

$('#cottageCal').datepicker({
    minDate: '0d',
    maxDate: lastDate,
    beforeShowDay: function(date) {
        $.each(cottageStart, function(key, value) { 
          //alert (((date >= value) && (date <= value)) ? 'booked' : 'notbooked');
            if ((date >= value) && (date <= value)) {
                return [false, 'booked'];
            } else {
                return checkPeak(date);
            }
        });
    }
});

Finally, the checkPeak function called from here looks like this:

var checkPeak = function(date) {
    var day = date.getDay();
    var month = date.getMonth();
    $.each(peakStart, function(key, value) { 
        if ((date > value) && (date < value)) {
        /* december peak bookings on monday*/
            if (month != 11) {
                return [(day == 5), ''];
            } else {
                return [(day == 1), ''];
            }
        }
        if (month == 0) {
            return false;
        } else {
            // it not during a peak period
            return [(day == 1 || day == 5), ''];
        }
    });
}

I have to do something fundamentally wrong, but I don’t see that!

+3
source share
2 answers

A rather old question, but I went through the same problem, landed here in search of a solution, and then I decided to pay more attention to the documentation :

beforeShowDay - () [0] true/false , , 1, CSS '' , [2] - . datepicker .

, , :

$('#cottageCal').datepicker({
    minDate: '0d',
    maxDate: lastDate,
    beforeShowDay: function(date) {
        $.each(cottageStart, function(key, value) { 
            if ((date >= value) && (date <= value)) {
                return new Array(false, 'booked');
            } else {
                return checkPeak(date);
            }
        });
    }
});

:

var checkPeak = function(date) {
    var day = date.getDay();
    var month = date.getMonth();
    var returnArr = new Array();
    $.each(peakStart, function(key, value) { 
        if ((date > value) && (date < value)) {
        /* december peak bookings on monday*/
            if (month != 11) {
                returnArr[0] = (day == 5);
                returnArr[1] = '';
                return returnArr;
            } else {
                returnArr[0] = (day == 1);
                returnArr[1] = '';
                return returnArr;
            }
        }
        if (month == 0) {
            returnArr[0] = false;
            return returnArr;
        } else {
            // it not during a peak period
            returnArr[0] = (day == 1 || day == 5);
            returnArr[1] = '';
            return returnArr;
        }
    });
}

!

+10

@Penzizzle

? 0d minDate:

minDate:'-1d'

.

0

Source: https://habr.com/ru/post/1794673/


All Articles