I have a custom appointment form with multiple locations. On Sundays all locations are closed. On Saturdays only a few locations are open. One location is so busy that all appointments need to be 1 week in advance at the very least.
I was finding answers saying, "You cannot update the current picker. You need to destroy it, set the reserved dates in an array, and rebuild the datepicker with those options.", which meant duplicating the datepicker that's on the custom form with all it's odd regional formats and options.
Then, after telling everyone it's going to be more work than it's worth I found out you can just out the next 7 days dynamically with:
$('input.datepicker').datepicker( 'option', 'minDate', '+7d');
That works! Then also found you could tell the current datepicker to switch to today's date with this:
$('input.datepicker').datepicker('setDate', 'today');
So can I get a live datepicker to hide weekends? I should be able to but the option I found fails:
$('input.datepicker').datepicker({ beforeShowDay: jQuery.datepicker.noWeekends });
I really want to be able to say, "no weekends" or "no sundays", without destroying and rebuilding, but it's not a popular topic since most forms don't need to update the datepicker dynamically.
Here's a simple fiddle of what I'd like to accomplish: https://jsfiddle.net/56qsau34/
EDIT: So I was really close but I had the wrong method(?) for applying an update to the live datepicker. If you take the last guess and write it properly it works!
$('input.datepicker').datepicker( 'option', 'beforeShowDay', jQuery.datepicker.noWeekends );
EDIT #2: Just for the poor soul that finds this without the final solution. When you realize that you need to 'toggle' the noWeekends feature this function that returns all the dates with 'true' will come in real handy:
$('input.datepicker').datepicker('option', 'beforeShowDay', function(d) {
var year = d.getFullYear(),
month = ("0" + (d.getMonth() + 1)).slice(-2),
day = ("0" + (d.getDate())).slice(-2);
var formatted = year + '-' + month + '-' + day;
//is it a sunday - this was the missing bit for all my specs
if (d.getUTCDay() != 0) {
return [true, "",""];
}else{
return [false, "",""];
}
}