最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i disable public holidays in a jQuery UI date picker? - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question edited Feb 27, 2014 at 10:10 dachi 1,60211 silver badges15 bronze badges asked Feb 27, 2014 at 10:06 Jimmy_ChongJimmy_Chong 1292 gold badges2 silver badges12 bronze badges 2
  • This should help... davidwalsh.name/jquery-datepicker-disable-days – Reinstate Monica Cellio Commented Feb 27, 2014 at 10:08
  • Duplicated of stackoverflow.com/questions/677976/… – monu Commented Feb 27, 2014 at 10:08
Add a comment  | 

2 Answers 2

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论