且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery UI Datepicker预选不是星期天的日期

更新时间:2023-01-28 21:08:44

假设您将切换到第二天,星期一,则可以使用条件(三元)运算符.

Assuming you would switch to the Next day, Monday, you can use conditional (ternary) operator.

这里是一个例子.

$(function() {
  $("#delivery-date").datepicker({
    minDate: new Date().getDay() + 2 == 7 ? 3 : 2,
    maxDate: '+2M',
    dateFormat: "DD, d MM yy",
    showOn: "both",
    buttonText: "Change",
    beforeShowDay: function(date) {
      var day = date.getDay();
      return [(day != 0), ''];
    }
  }).datepicker("setDate", "0");
});

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Date: <input type="text" id="delivery-date"></p>

如果今天是星期五(5),则加上2天,则将其设为下周的星期日(7,或JS:0).所以我们可以检查一下.

If today is Friday (5), adding 2 days, would make it Sunday of the next week (7, or in JS: 0). So we can check for that.

通常我们可以创建一个函数:

Normally we might make a function:

function pickDay(n){
  var d = new Date().getDay();  // Returns the day of the week: 5
  if(d + n == 7){
    return n + 1;
  }
  return n;
}

然后像这样使用它:

minDate: pickDay(2),

我对此进行了测试,但它不受欢迎,似乎存在一些计时问题.

I tested this and it's not liked, there seems to be some timing issue.