在 jQuery 日期选择器上禁用星期一和星期六

Disable mondays and saturdays on jQuery datepicker

我需要 jQuery 日期选择器来禁用一组特定日期以及所有星期一和所有星期六。我已经实现了前两个目标,但没有实现第三个目标(禁用星期六)。这是代码:

let fechasEspecificas = ["2018-11-06"]

jQuery.datepicker.setDefaults({
  "minDate": 2,
  "maxDate": new Date(2019,0,31),
  beforeShowDay: function(date) {
      let string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(fechasEspecificas,string)) {
        return [false, '']  // here are specific dates disabled
      } else {
        let day = date.getDay();
        return [(day != 1), '']; // here are mondays disabled
      }
    }
});


function contains(a, obj) {var i = a.length;while (i--) {if (a[i] === obj){return true;}}return false;}

JSFIDDLE demo

如何扩展代码以禁用星期六?

您应该可以将 return [(day != 1), '']; 更改为 return [(day != 1 && day != 6), ''];

完整代码:

let fechasEspecificas = ["2018-11-06"]

jQuery.datepicker.setDefaults({
    "minDate": 2,
  "maxDate": new Date(2019,0,31),
    beforeShowDay: function(date) {
      let string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(fechasEspecificas,string)) {
        return [false, '']
      } else {
        let day = date.getDay();
        return [(day != 1 && day != 6), ''];
      }
    }
});


function contains(a, obj) {var i = a.length;while (i--) {if (a[i] === obj){return true;}}return false;}


jQuery('input').datepicker();

这将禁用星期一和星期六。

您必须将要禁用的日期与 AND 运算连接起来

let fechasEspecificas = ["2018-11-06"]

jQuery.datepicker.setDefaults({
    "minDate": 2,
  "maxDate": new Date(2019,0,31),
    beforeShowDay: function(date) {
      let string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(fechasEspecificas,string)) {
        return [false, '']
      } else {
        let day = date.getDay();
        return [(day != 1) && (day != 6), '']; //Add the day number 6 for disable saturdays as well
      }
    }
});


function contains(a, obj) {var i = a.length;while (i--) {if (a[i] === obj){return true;}}return false;}


jQuery('input').datepicker();