4

如何让 jQuery UI datepicker 排除周末并设置minDate为某个值,例如 +2 / +3(不包括周末)?

我试过的:

$("#txtFromDate").datepicker({
    minDate: 4, 
    beforeShowDay: $.datepicker.noWeekends,
    changeMonth: true,
    changeYear: true
});
<input type="text" id="txtFromDate" />

例如,当我选择星期一(今天 10 日)并将 'minDate' 设为 10 时,应从 24 日起启用。

谁能帮帮我?我希望能够在minDate不包括周六和周日的情况下计算该值。

小提琴-http ://jsfiddle.net/betrob/7DHVr/2/

谢谢

4

2 回答 2

4

您可以手动进行计算,然后像这样将下一个工作日传递给 minDate。

// Calculate the next working day manually
    var startDate = new Date(),
    noOfDaysToAdd = 10,
    count = 1;

while(count <= noOfDaysToAdd){
    startDate.setDate(startDate.getDate() + 1);
    if(startDate.getDay() != 0 && startDate.getDay() != 6){
        count++;
    }
}

// Datepicker Init
$('#txtFromDate').datepicker({
    minDate: startDate,
    beforeShowDay: $.datepicker.noWeekends,
    changeMonth: true,
    changeYear: true
});

这是一个工作演示。演示

于 2014-02-10T06:28:36.600 回答
3

我试图优化@Shiva 逻辑以使其更快。希望对您有所帮助,如有任何改进或漏洞,请随时指出。:)

演示

var count = 10;
//Add the 2 days of weekend in numer of days .
var d = new Date();
count = count + (parseInt(count/5))*2;
d.setDate(d.getDate() +count);
//suppose its ending on weekend day then increment them manually.
if(d.getDay()>5) {  d.setDate(d.getDate()+ (d.getDay()-5)) ; } 

$(document).ready(function () {
   $("#txtFromDate").datepicker(
       { minDate: d, 
                beforeShowDay: $.datepicker.noWeekends,
                changeMonth: true,
                changeYear: true});
 });

更新:更优化一点。

于 2014-02-10T08:42:58.620 回答