I have a hotel reservation form where I have two fields called checkIn and checkOut. I use jQuery datepicker to book rooms here. I do not want to show the dates that are already booked. I tried like this.
$(function() { var excludedCheckInDates = CHECKINDATES; // an array of already booked checkin dates var excludedCheckOutDates = CHECKOUTDATES; // an array of already booked checkout dates $.datepicker .setDefaults({ defaultDate: '+1w', changeMonth: true, changeYear: true, minDate: 0, beforeShowDay: function(date) { date = $.datepicker.formatDate('yy-mm-dd', date); excludedCheckInDates = $.inArray(date, excludedCheckInDates) < 0; excludedCheckOutDates = $.inArray(date, excludedCheckOutDates) < 0; if (excludedCheckInDates) { return [true, 'selectedDate']; } else { return false; } if (excludedCheckOutDates) { return true; } else { return false; } return true; } }); $('#checkIn').datepicker({ onSelect: function(selectedDate) { $('#checkIn').datepicker('option', 'minDate', selectedDate || 0); } }); $('#checkOut').datepicker({ onSelect: function(selectedDate) { $('#checkOut').datepicker('option', 'maxDate', selectedDate); } }); });
source share