在Datepicker中,我试图将可选日期限制为星期日,并且只有在星期四15:00之前选择。 我做错了什么?,仅限于周日作品,截止时间为15:00,但前一天不是前两天。
$( "#deliverysunday" ).datepicker({
dateFormat: "yy-mm-dd",
showWeek: true,
firstDay: 1,
beforeShowDay: function(date){ return [date.getDay() == 0,""]},
minDate: new Date().getHours() >= 15 ? 2 : 0
});
答案 0 :(得分:0)
如果我正确理解您的问题,您需要确保选择的最早日期是不迟于周四15:00发生的第一个星期日。
minDate
选项将控制哪个日期是最早的可选日期;确定截止时间的逻辑应该在beforeShowDay
选项中:
$( "#deliverysunday" ).datepicker({
dateFormat: "yy-mm-dd",
showWeek: true,
firstDay: 1,
beforeShowDay: function(date) {
return [isDateSelectable(date)];
},
minDate: 2
});
/**
* Given a date, returns a boolean to indicate
* whether the date should be selectable in a picker.
* Tests the date according to the rules:
*
* - Is the date a Sunday?
* - Is it currently before the cutoff time?
*/
function isDateSelectable(date) {
// Cutoff interval = Sun@00:00 - Thu@15:00 = 57 hours
var intervalInMillis = 57 * 60 * 60 * 1000;
var now = new Date();
var isPriorToInterval = now.getTime() < date.getTime() - intervalInMillis;
// Is date a Sunday?
var isSunday = date.getDay() === 0;
return isSunday && isPriorToInterval;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<h1>Date picker example</h1>
<div id="deliverysunday"></div>