Search code examples
jqueryjquery-ui-datepickermindate

Jquery UI Datpicker excluding weekends minDate


How do I make jQuery UI datepicker exclude weekends and also set minDate to a certain value like +2 / +3 (excluding weekends) ???

What I've tried:

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

For example When I select a Monday (like today 10th) and give 'minDate' as 10 , from 24th onwards should be enabled.

Can anyone please help me out? I want to be able to calculate the minDate value without including Saturdays and Sundays.

FIDDLE- http://jsfiddle.net/betrob/7DHVr/2/

Thank you


Solution

  • I have tried to optimize @Shiva logic to make it more faster. Hope its help, please feel free to point out any improvements or loopholes. :)

    DEMO

    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});
     });
    

    Update: A bit more optimized.