Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?

前端 未结 11 795
無奈伤痛
無奈伤痛 2020-11-22 04:02

I use a datepicker for choosing an appointment day. I already set the date range to be only for the next month. That works fine. I want to exclude Saturdays and Sundays f

11条回答
  •  独厮守ぢ
    2020-11-22 04:31

    In this version, month, day, and year determines which days to block on the calendar.

    $(document).ready(function (){
      var d         = new Date();
      var natDays   = [[1,1,2009],[1,1,2010],[12,31,2010],[1,19,2009]];
    
      function nationalDays(date) {
        var m = date.getMonth();
        var d = date.getDate();
        var y = date.getFullYear();
    
        for (i = 0; i < natDays.length; i++) {
          if ((m == natDays[i][0] - 1) && (d == natDays[i][1]) && (y == natDays[i][2]))
          {
            return [false];
          }
        }
        return [true];
      }
      function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
          if (noWeekend[0]) {
            return nationalDays(date);
          } else {
            return noWeekend;
        }
      }
      $(function() { 
        $(".datepicker").datepicker({
    
          minDate: new Date(d.getFullYear(), 1 - 1, 1),
          maxDate: new Date(d.getFullYear()+1, 11, 31),
    
          hideIfNoPrevNext: true,
          beforeShowDay: noWeekendsOrHolidays,
         });
      });
    });
    

提交回复
热议问题