简体   繁体   English

jQuery Datepicker - 如何在今天之后的前两天禁用

[英]jQuery Datepicker - How to disable first two days after today

I am trying to make a jQuery datepicker where the first two days after today are disabled. 我正在尝试制作一个jQuery datepicker,其中今天的前两天被禁用。

What I mean by this is for example, the current date today is the 12th (Monday). 我的意思是,例如,今天的当前日期是12日(星期一)。 When a customer clicks on the calendar the 12th and 13th are both greyed out and unclickable and the first available day is the 14th (Wednesday) 当客户点击日历时,第12天和第13天都是灰色且无法点击,第一个可用日期是第14天(周三)

I currently only have a basic calendar with weekends disabled and have not been able to find a way to do this method. 我目前只有一个基本日历,周末禁用,并且无法找到方法来执行此方法。

Any help would be appreciated. 任何帮助,将不胜感激。

You can use the option beforeShowDay with a function that returns false for today's and tomorrow's date 您可以在beforeShowDay使用选项,该函数在今天和明天的日期返回false

 $(function() { $("#datepicker").datepicker({ beforeShowDay: function(date) { var today = new Date().getDate(), tomorrow = today + 1; // date is not today, tomorrow, or week-end if ((today == date.getDate() || tomorrow == date.getDate()) || date.getDay() % 6 == 0) { return [false, "CSSclass", "disabled"]; } else { return [true, '', '']; } } }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script> <p>Date: <input type="text" id="datepicker"></p> 

You may want something along the lines of this jsfiddle 你可能想要这个jsfiddle的东西

$(function() {
  var date = new Date();
  var minDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 2);
  $('#thedate').datepicker({
    dateFormat: 'dd-mm-yy',
    minDate: minDate,
    beforeShowDay: $.datepicker.noWeekends
  });

});

It gets the current date and creates a new one with the date 2 days ahead. 它获取当前日期并创建一个新日期,提前2天。 Then it uses the minDate property of date picker and sets it to your new min date. 然后它使用日期选择器的minDate属性并将其设置为新的最小日期。 You may have to do checks to see if the +2 is greater than the end of the month, but it shouldn't be a big hassle. 您可能需要检查以确定+2是否大于月末,但这不应该是一个很大的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM