Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?

前端 未结 11 685
無奈伤痛
無奈伤痛 2020-11-22 04:02

I use a datepicker for choosing an appointment day. I already set the date range to be only for the next month. That works fine. I want to exclude Saturdays and Sundays f

11条回答
  •  遇见更好的自我
    2020-11-22 04:53

    These answers were very helpful. Thank you.

    My contribution below adds an array where multiple days can return false (we're closed every Tuesday, Wednesday and Thursday). And I bundled the specific dates plus years and the no-weekends functions.

    If you want weekends off, add [Saturday], [Sunday] to the closedDays array.

    $(document).ready(function(){
    
        $("#datepicker").datepicker({
            beforeShowDay: nonWorkingDates,
            numberOfMonths: 1,
            minDate: '05/01/09',
            maxDate: '+2M',
            firstDay: 1
        });
    
        function nonWorkingDates(date){
            var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
            var closedDates = [[7, 29, 2009], [8, 25, 2010]];
            var closedDays = [[Monday], [Tuesday]];
            for (var i = 0; i < closedDays.length; i++) {
                if (day == closedDays[i][0]) {
                    return [false];
                }
    
            }
    
            for (i = 0; i < closedDates.length; i++) {
                if (date.getMonth() == closedDates[i][0] - 1 &&
                date.getDate() == closedDates[i][1] &&
                date.getFullYear() == closedDates[i][2]) {
                    return [false];
                }
            }
    
            return [true];
        }
    
    
    
    
    });
    

提交回复
热议问题