Reputation: 6099
I need to skip out weekends on the DatePicker calendar. The first post below helped me to figure out how to block out weekends, however, this on it's own will not work for what I need.
Let me give a couple of examples:
In other words there should always be 3 days available to select and it should be weekdays only.
<script>
//initialise datepicker
$(function() {
$( ".datepicker").datepicker({
dateFormat: 'dd/mm/yy',
minDate: 0,
maxDate: 2
});
/*
$('body').on('focus',".datepicker", function(){
$(this).datepicker({dateFormat: 'dd/mm/yy'});
})*/
$('.cal').on('click', function(e) {
var target = $(this).closest('.calendar').find('.datepicker');
target.datepicker('show');
});
});
</script>
Upvotes: 2
Views: 2132
Reputation: 2865
I think this is a very dirty hack that you can come over with:
$("#datepicker").datepicker({
dateFormat: 'dd/mm/yy',
beforeShowDay: $.datepicker.noWeekends,
minDate: 0,
maxDate: (function () {
var today = new Date().getDay(), add = 0;
switch (today) {
case 0:
add = 1;
break;
case 4:
case 5:
case 6:
add = 2;
break;
}
return 2 + add;
})()
});
Upvotes: 4
Reputation: 14862
Assuming you are using jQuery UI Datepicker (other datepickers are available):
$( "#datepicker" ).datepicker({
dateFormat: 'dd/mm/yy',
minDate: 0,
maxDate: 2,
beforeShowDay: $.datepicker.noWeekends
});
Link to documentation.
beforeShowDay
accepts a function as callback to determine which days to show, with the function returning either true
or false
based on the day. For example, it is possible to supply a custom function to exclude only Sundays.
Upvotes: 1