I realize many have already asked similar questions, but I have been plugging away at this for ages and, despite reviewing numerous posts and multiple attempts, I can't figure out how to get it working.
I'm using an ajax call to retrieve an array of dates that I want to remove as an option from my date field. The values are formatted as 'yyyy-mm-dd'. I will present this code as an example:
// minDate is a date value, set to 2022-08-05
// maxDate is a date set to 2023-01-29
// 'calendar' is a jquery object representing a date field
var unavailableDates = ["2022-08-06","2022-08-07","2022-08-12"] //this is a reduced/simplified version
calendar.attr('min', minDate);
calendar.attr('max', maxDate);
Everything up to here works just fine. If I leave it at that, the min and max work as expected. However, as soon as I try and add anything using beforeShowDay, it all falls apart. If I inspect the element after trying to use beforeShowDay, I can still see the min and max attributes, but the datepicker no longer shows them as disabled. Here are a few different things I've tried:
const beforeShowDayHandler = function (date) {
// check appt available
if (unavailableDates.indexOf(date) >= 0)
return [false, '', ''];
return [true, '', ''];
}
calendar.attr('min', minDate);
calendar.attr('max', maxDate);
calendar.datepicker('option', 'beforeShowDay', beforeShowDayHandler);
Or
calendar.datepicker('option', 'beforeShowDay', function(date){
if (unavailableDates.indexOf(date) >=0) //along with a couple versions of date.toString
return [false, '', ''];
return [true, '', ''];
});
calendar.datepicker({
minDate: minDate,
maxDate: maxDate,
beforeShowDay: function (date) {
debugger;
// check appt available
if (unavailableDates.indexOf(date.toDateString()) >= 0){
return [false, '', ''];
}
else {
return [true, '', ''];
}
}
});
And a few others. So far, none of them have worked. I've never used this before (obviously) - what am I doing wrong?