我试图让datepicker只显示两个特定日期之间的周末。我所拥有的代码仅显示周末,或仅显示日期之间可用,但不显示预期结果。这就是我现在所拥有的:
$(function() {
$('.datepicker').datepicker({
"dateFormat": 'yy/mm/dd',
'minDate': new Date(),
beforeShowDay: function(dt) {
if (dt.getMonth() > 3 && dt.getMonth() < 8) {
return [dt.getDay() == 0 || dt.getDay() == 6, ""];
} else {
return [true, "", "Available"];
}
}
});
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<input type="text" class="datepicker">
&#13;
这只显示了周末可用,但现在我需要将它限制在5月27日到8月17日之间的周末以及8月17日之后的一切。有谁知道怎么做?
答案 0 :(得分:1)
添加另一个嵌套if部分以定义几个月内的日期。如果您想要包含日期,只需将其设为<=
或>=
,而不仅仅是<
和>
$(function() {
$('.datepicker').datepicker({
"dateFormat": 'yy/mm/dd',
'minDate': new Date(),
beforeShowDay: function(dt) {
if (dt.getMonth() > 3 && dt.getMonth() < 8) {
if (dt.getMonth() == 4 && dt.getDate() < 26) {
return [true, "", "Available"];
}
if (dt.getMonth() == 7 && dt.getDate() > 17) {
return [true, "", "Available"];
}
return [dt.getDay() == 0 || dt.getDay() == 6, ""];
}
else {
return [true, "", "Available"];
}
}
});
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<input type="text" class="datepicker">
&#13;