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
You can do the calculation manually and pass the next working day to the minDate like this.
// 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
});
Here is a working demo. DEMO
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With