如何在 jQuery UI Datepicker 中禁用下一个周末?

How to Disable the very next weekends in jQuery UI Datepicker?

我想在一个订单页面中实现一个日历,如果用户在星期五上午 10 点之后订购,则在交货日期日历中屏蔽下一个星期六和星期日。这是我正在尝试但未按预期工作的示例代码。

beforeShowDay: function(date) {
    var day = dt.getDay();
    var hour = dt.getHours();
    if (day == 4) {
        // i think, here i want to put the code to disable days
    }
}

如果我用这样的东西

beforeShowDay: function(date) {
    var day = date.getDay();
    var dt = new Date();
    var hour = dt.getHours();
    return [(day != 5 && day != 6)];
}

我可以禁用周六和周日,但这会禁用所有周六和周日。我只想禁用接下来的周六周日禁用。我也可以在 var hour 中获取当前小时数,那么我应该在哪里使用条件来检查小时数是否大于上午 10 点,我正在使用类似的东西但没有工作

beforeShowDay: function(date) {
    var dt = new Date();
    var hour = dt.getHours();
    var day = date.getDay();
    if (day == 4 && hour >= 10) {
        return [(day != 5 && day != 6)];
    }
}

beforeShowDayType: Function( Date date )

Default: null

A function that takes a date as a parameter and must return an array with:

[0]: true/false indicating whether or not this date is selectable [1]: a CSS class name to add to the date's cell or "" for the default presentation [2]: an optional popup tooltip for this date

The function is called for each day in the datepicker before it is displayed.

beforeShowDay: function(date) {
     var day = dt.getDay();
                         var hour = dt.getHours();
                        if( day == 4) {
                           // this is an example of how to use
                            //first parameter is disable or not this date
                            //second parameter is the class you want to add ( jquery will remove the click listener, but you probebly want to style it like this date is not available
                            //third optional tootltip like 'closed on weekends'
                             return [false,'disabled','weekend']
                        }

                    }     
$('#datepicker').datepicker({
    beforeShowDay: function(date){
        var dt = new Date(),
            day = dt.getDay(),
            hour = dt.getHours(),
            twoDaysFrmNow = new Date().setDate(dt.getDate() + 2);
        return [!(day == 5 && hour >= 10 && date <= twoDaysFrmNow &&  date > dt)];
    }
});

beforeShowDay 函数中,检查当前日期是否为星期五以及上午 10 点之后。如果这是真的,那么您还需要检查作为参数传递的 date 是否是下周六或周日:

$(function() {
  $("#datepicker").datepicker({
    beforeShowDay: function(date) {
      // date (Friday March 13 2015 10:00 AM) is hardcoded for testing
      var now = new Date(2015, 3 - 1, 13, 10, 0, 0, 0);
      if (now.getDay() === 5 && now.getHours() >= 10) {
        var now_plus_1 = new Date(now.getTime()); now_plus_1.setHours(0, 0, 0, 0); now_plus_1.setDate(now_plus_1.getDate() + 1);
        var now_plus_2 = new Date(now.getTime()); now_plus_2.setHours(0, 0, 0, 0); now_plus_2.setDate(now_plus_2.getDate() + 2);
        return [date.getTime() !== now_plus_1.getTime() && date.getTime() !== now_plus_2.getTime(), ""];
      }
      return [true, ""];
    }
  });
});
@import url("//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/ui-darkness/jquery-ui.min.css");

body { font-size: smaller; }
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<input id="datepicker">