JS Datepicker:禁用过去日期+一周中的特定日期+特定日期

时间:2018-01-09 11:58:07

标签: javascript jquery datepicker

我在JS中有一个日期选择器,我禁用了传递日期,只允许这样的星期六:

$(document).ready(function(){
    $("#aankomstdatum").datepicker({
        dateFormat: "dd-mm-yy",
        numberOfMonths:2,
        minDate: 0,
        beforeShowDay: function(date){
        var day = date.getDay();
        return [day == 6];
    }});
});

我还有一个代码可以让我禁用这样的特定日期:

 /** Days to be disabled as an array */
var disableddates = ["26-05-2018"];

function DisableSpecificDates(date) {

 var m = date.getMonth();
 var d = date.getDate();
 var y = date.getFullYear();

 // First convert the date in to the mm-dd-yyyy format 
 // Take note that we will increment the month count by 1 
 var currentdate = (m + 1) + '-' + d + '-' + y ;

  // We will now check if the date belongs to disableddates array 
 for (var i = 0; i < disableddates.length; i++) {

 // Now check if the current date is in disabled dates array. 
 if ($.inArray(currentdate, disableddates) != -1 ) {
 return [false];
 }
 }
}

添加它,它可以工作:

beforeShowDay: DisableSpecificDates

我遇到的问题是我不能同时工作。我不确定如何禁用过去的日期和除星期六之外的所有日子并且还禁用数组中的特定给定日期,而单独它们确实有效。尝试例如时,我总是会遇到语法错误:

    beforeShowDay: DisableSpecificDates, function(date){
    var day = date.getDay();
    return [day == 6];
}});

这可能吗?

1 个答案:

答案 0 :(得分:1)

是的,有可能实现这一目标。如果出现以下情况,您想创建一个函数并从中返回[false]

  1. date是星期六或
  2. date包含在disableddates数组
  3. 以下是示例:

    var disableddates = ["26-04-2018"];
    
    function DisableDates(date) {
      var selectable = !isSaturday(date) && !isDateDisabled(date);
    
      return [selectable];
    }
    
    function isSaturday(date) {
      var day = date.getDay();
    
      return day === 6;
    }
    
    function isDateDisabled(date) {
      var m = date.getMonth() + 1;
      var d = date.getDate();
      var y = date.getFullYear();
    
      // First convert the date in to the dd-mm-yyyy format
      if(d < 10) d = '0' + d;
      if(m < 10) m = '0' + m;
    
      var currentdate = d + '-' + m + '-' + y;
    
      // Check if disableddates array contains the currentdate
      return disableddates.indexOf(currentdate) >= 0;
    }
    

    然后只需将DisableDates函数作为beforeShowDay选项的值传递:

    $("#aankomstdatum").datepicker({
      dateFormat: "dd-mm-yy",
      numberOfMonths:2,
      minDate: 0,
      beforeShowDay: DisableDates
    });