7

I'm making a holiday booking application and obviously you don't need to book off holidays that are already given to you, so I need to know how I can disable, Christmas, for example from the date picker every year without me having to change the code every year.

This is my jQuery UI date picker code so far:

<script>
$(function() {
  $("#from").datepicker({
      beforeShowDay: $.datepicker.noWeekends,
      defaultDate: "today",
      changeMonth: true,
      numberOfMonths: 1,
      minDate: "today",
      dateFormat: "dd/mm/yy",
      onClose: function(selectedDate) {
        $( "#to" ).datepicker("option", "minDate", selectedDate);
      }
  });
  $("#to").datepicker({
    beforeShowDay: $.datepicker.noWeekends,
    defaultDate: "today",
    changeMonth: true,
    numberOfMonths: 1,
    minDate: "today",
    dateFormat: "dd/mm/yy",
    onClose: function(selectedDate) {
      $("#from").datepicker("option", "maxDate", selectedDate);
    }
  });
});
</script>
dachi
  • 1,604
  • 11
  • 15
Jimmy_Chong
  • 109
  • 2
  • 2
  • 12

2 Answers2

16

you can exclude the dates like

var holidays = ["2014-02-27","2014-02-01"];
$( "#from" ).datepicker({
beforeShowDay: function(date){
    var datestring = jQuery.datepicker.formatDate('yy-mm-dd', date);
    return [ holidays.indexOf(datestring) == -1 ]
}
});

you can provide more dates to the holidays array

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
  • 1
    is there anyway to just have like the 25th of the 12 disabled every year because if I do it this way ill have to either change the code every year or input a lot of dates into the array – Jimmy_Chong Feb 27 '14 at 11:05
  • Hi how about if i set Max date? does that exclude the holidays? – Suvin94 Nov 29 '19 at 04:31
1

You have to use beforeShowDay attribute of DatePicker like below:

$("#textboxid").datepicker({
beforeShowDay: function(date) {
var day = date.getDay();
return [day != 0,''];
}
})​​​​​;​

Above script will disable all Sundays.

Kapil Kshirsagar
  • 282
  • 1
  • 4
  • 19