Home > Web Front-end > JS Tutorial > body text

How to Disable Weekends and Holidays in jQuery UI Datepicker?

Patricia Arquette
Release: 2024-11-18 07:20:02
Original
539 people have browsed it

How to Disable Weekends and Holidays in jQuery UI Datepicker?

Disabling Saturdays, Sundays, and Holidays in jQuery UI Datepicker

The jQuery UI Datepicker offers extensive customization options to cater to specific requirements. One common need is to disable certain days of the week or specific dates from the selection range.

Disabling Weekends

To prevent users from selecting weekend days (Saturdays and Sundays), you can leverage the built-in $.datepicker.noWeekends function:

$(".selector").datepicker({
  beforeShowDay: $.datepicker.noWeekends
});
Copy after login

Disabling Holidays

To exclude specific holidays from the selection, you can create a custom function like:

function nationalDays(date) {
  // Replace [YOUR_HOLIDAY_DATES] with the specific dates you want to disable.
  if ([YOUR_HOLIDAY_DATES].includes(date.toString())) {
    return [false, 'holiday-day'];
  }
  return [true, ''];
}
Copy after login

Combining Weekend and Holiday Disabling

To disable both weekends and holidays, you can combine the two functions:

function noWeekendsOrHolidays(date) {
  var noWeekend = $.datepicker.noWeekends(date);
  if (noWeekend[0]) {
    return nationalDays(date);
  } else {
    return noWeekend;
  }
}
Copy after login

Then apply the combined function to the Datepicker:

$(".selector").datepicker({
  beforeShowDay: noWeekendsOrHolidays
});
Copy after login

Note: In jQuery UI 1.8.19 and later, the beforeShowDay option supports an optional third parameter for displaying a tooltip on disabled dates.

The above is the detailed content of How to Disable Weekends and Holidays in jQuery UI Datepicker?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template