我需要在接下来的三个工作日内展示。我使用了以下代码。
$('#txtDate').datepicker({
minDate: 0,
maxDate: '+3',
beforeShowDay: $.datepicker.noWeekends
});
工作正常。但有些条件不满意。例如,今天的日期为12-03-2016,接下来的3个工作日意味着它只显示2天
答案 0 :(得分:0)
试试这个。你可以优化这个逻辑,如果它可以帮助你
$('#txtDate').datepicker({
minDate: 0,
stepMonths: 0,
beforeShowDay: function(d) {
var today = new Date(); //new Date("Mar 15 2016");
if (d.getDay() === 0) {
return [false];
} else if ((today.getDay() >= 0 && today.getDay() < 4) && (d.getDate() == today.getDate() + 1 || d.getDate() == today.getDate() + 2 || d.getDate() == today.getDate() + 3)) {
return [true];
} else if (today.getDay() == 4 && (d.getDate() == today.getDate() + 1 || d.getDate() == today.getDate() + 2 || d.getDate() == today.getDate() + 4)) {
return [true];
} else if (today.getDay() == 5 && (d.getDate() == today.getDate() + 1 || d.getDate() == today.getDate() + 3 || d.getDate() == today.getDate() + 4)) {
return [true];
} else if (today.getDay() == 6 && (d.getDate() == today.getDate() + 2 || d.getDate() == today.getDate() + 3 || d.getDate() == today.getDate() + 4)) {
return [true];
} else {
return [false];
}
}
});
&#13;
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input type=text id='txtDate' />
&#13;
一周中所有日期的示例
$('.txtDate').datepicker({
minDate: 0,
stepMonths: 0,
beforeShowDay: function(d) {
var today = new Date($(this).val());
if (d.getDay() === 0) {
return [false];
} else if ((today.getDay() >= 0 && today.getDay() < 4) && (d.getDate() == today.getDate() + 1 || d.getDate() == today.getDate() + 2 || d.getDate() == today.getDate() + 3)) {
return [true];
} else if (today.getDay() == 4 && (d.getDate() == today.getDate() + 1 || d.getDate() == today.getDate() + 2 || d.getDate() == today.getDate() + 4)) {
return [true];
} else if (today.getDay() == 5 && (d.getDate() == today.getDate() + 1 || d.getDate() == today.getDate() + 3 || d.getDate() == today.getDate() + 4)) {
return [true];
} else if (today.getDay() == 6 && (d.getDate() == today.getDate() + 2 || d.getDate() == today.getDate() + 3 || d.getDate() == today.getDate() + 4)) {
return [true];
} else {
return [false];
}
}
});
&#13;
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input type=text class='txtDate' value='Mar 13 2016' />
<input type=text class='txtDate' value='Mar 14 2016' />
<input type=text class='txtDate' value='Mar 15 2016' />
<input type=text class='txtDate' value='Mar 16 2016' />
<input type=text class='txtDate' value='Mar 17 2016' />
<input type=text class='txtDate' value='Mar 18 2016' />
<input type=text class='txtDate' value='Mar 19 2016' />
&#13;