V4n1ll4
V4n1ll4

Reputation: 6099

DatePicker block out weekends

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:

  1. Lets say today is Monday. The best 3 options will show Monday, Tuesday & Wednesday.
  2. Now lets say today is Thursday. The next 3 options shown on the calendar will be Thursday, Friday & Monday.
  3. Finally lets say today is Saturday. The next 3 options shown on the calendar will be Monday, Tuesday, Wednesday.

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

Answers (2)

iamawebgeek
iamawebgeek

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

Richard Parnaby-King
Richard Parnaby-King

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

Related Questions