0

My code excludes dates, assigns a custom class and a title to the date field.

var holidays =[ [2020,06,17,'A Holiday'],[2020,06,18,'Another Holiday'],[2020,06,19,'Some Other Holiday'] ];

function setHolidays(date) {
    for (i = 0; i < holidays.length; i++) {
        if (date.getFullYear() == holidays[i][0]
            && date.getMonth() == holidays[i][1] - 1
            && date.getDate() == holidays[i][2]) {
            return [false, 'holiday', holidays[i][3]];
        }
    }
    return [true, ''];
}

$( ".add_delivery_date").datepicker( {
    minDate: 0, 
    firstDay: 0,
    beforeShowDay: setHolidays
} );

I know I can exclude weekends by using

beforeShowDay: $.datepicker.noWeekends

but I am unsure how to add it to the existing code.

Junky
  • 958
  • 7
  • 17
  • I realise you have already solved the problem, but AFAICT both the question and answer are duplicates of other questions here on SO, eg https://stackoverflow.com/questions/501943/can-the-jquery-ui-datepicker-be-made-to-disable-saturdays-and-sundays-and-holid, https://stackoverflow.com/questions/4561314/jquery-ui-datepicker-be-made-to-disable-weekends-and-holidays ... – Don't Panic Jun 17 '20 at 23:30
  • 1
    Does this answer your question? [Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?](https://stackoverflow.com/questions/501943/can-the-jquery-ui-datepicker-be-made-to-disable-saturdays-and-sundays-and-holid) – Don't Panic Jun 17 '20 at 23:30
  • This answer is very useful, thanks. – Junky Jun 18 '20 at 12:32

2 Answers2

0

You can simply pass noWeekends: true.

$( ".add_delivery_date").datepicker( {
    noWeekends: true,
    minDate: 0, 
    firstDay: 0,
    beforeShowDay: setHolidays
} );
imvain2
  • 15,480
  • 1
  • 16
  • 21
0

It was easier than I thought:

function setHolidays(date) {
    for (i = 0; i < holidays.length; i++) {
        if (date.getFullYear() == holidays[i][0]
            && date.getMonth() == holidays[i][1] - 1
            && date.getDate() == holidays[i][2]) {
            return [false, 'holiday', holidays[i][3]];
        }
    }

    var noWeekend = $.datepicker.noWeekends(date);
    return !noWeekend[0] ? noWeekend : [true];
}
Junky
  • 958
  • 7
  • 17