Senger CodeLab 🚀

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

September 29, 2026

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

Building intuitive web forms is crucial for user experience, especially when dealing with date inputs. The jQuery UI Datepicker is a robust and highly customizable tool that simplifies this task, offering a user-friendly interface for date selection. However, many applications, such as booking systems for medical appointments, travel, or services, require users to select only valid business days, excluding weekends and public holidays. This leads to a common and critical question for developers: Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)? The answer is a resounding yes, and it leverages the Datepicker’s powerful customization options to ensure only appropriate dates are selectable, enhancing form accuracy and preventing booking errors.

Understanding the Power of jQuery UI Datepicker Customization

The jQuery UI Datepicker is not just a simple calendar; it’s a versatile component designed for extensive customization. Its flexibility is largely due to a powerful callback function named beforeShowDay. This function is executed for each day rendered in the calendar, providing an opportunity to dynamically control its appearance and interactivity. By understanding how to leverage beforeShowDay, developers gain precise control over which dates are selectable and how they appear to the user.

The beforeShowDay function accepts a single argument: the JavaScript Date object for the day currently being rendered. It expects to return an array of three elements: a boolean indicating whether the day is selectable, a string for any custom CSS classes to apply to the day’s cell, and a string for the tooltip text. This granular control allows for complex logic to be applied to individual dates, making it an indispensable tool for tailored date selection experiences. For instance, a booking system might use this to grey out fully booked days or highlight promotional dates, alongside disabling weekends.

Effective use of this function ensures that your datepicker aligns perfectly with your application’s business rules. Whether you need to block out specific periods, enforce lead times, or, as our primary focus suggests, disable weekend selection, the beforeShowDay callback is your gateway to achieving these requirements. It’s a core component for advanced datepicker customization, allowing developers to create highly interactive and intelligent date input fields that guide users toward valid selections.

Disabling Weekends: A Step-by-Step Guide

Disabling Saturdays and Sundays in the jQuery UI Datepicker is a common requirement and can be achieved quite elegantly using the beforeShowDay option. This involves a small JavaScript function that checks the day of the week for each date rendered by the calendar. The JavaScript Date object’s getDay() method is instrumental here, returning an integer from 0 (Sunday) to 6 (Saturday). Our logic will focus on these values to identify and disable weekends.

Implementing this feature enhances user experience by preventing invalid selections from the outset. For services that only operate on weekdays, this immediately streamlines the booking process, reducing potential frustration and support queries. According to a study by Nielsen Norman Group, intuitive form design, including intelligent date pickers, significantly contributes to user satisfaction and task completion rates.

Here’s how you can disable weekends using the beforeShowDay callback:

  1. Initialize your Datepicker: Start by selecting your input field and calling the datepicker() method.
  2. Define the beforeShowDay function: Inside the datepicker() options, add the beforeShowDay property and assign it a function that takes a date parameter.
  3. Check the day of the week: Within this function, use date.getDay() to get the day of the week. Sunday is 0, Monday is 1, …, Saturday is 6.
  4. Return the appropriate array: If the day is Saturday (6) or Sunday (0), return [false, ''] to disable it. For all other days, return [true, ''] to enable them.
$(function() { $("datepicker").datepicker({ beforeShowDay: function(date) { var day = date.getDay(); // Disable Saturday (6) and Sunday (0) return [(day != 0 && day != 6)]; } }); }); 

This code snippet effectively disables weekend selection, ensuring that only weekdays are available for user interaction. It’s a fundamental step in customizing your datepicker for business-day-specific applications.

Integrating Public Holidays: Beyond Weekends

While disabling weekends addresses a significant portion of restricted date selection, many applications also require the ability to disable public holidays. Integrating public holidays into your jQuery UI Datepicker involves a slightly more complex but equally manageable extension of the beforeShowDay function. This ensures that your scheduling system respects all non-working days, providing a robust solution for appointment setting or project deadlines. Managing a list of holidays, whether static or dynamically fetched, is key to this functionality.

For applications where holiday observance is critical, such as government services, banks, or educational institutions, accurately disabling these dates is paramount. Failing to do so can lead to missed appointments, service disruptions, and customer dissatisfaction. The ability to disable specific dates based on a predefined list makes the jQuery UI Datepicker an incredibly flexible tool for professional-grade applications.

To incorporate holidays, you’ll typically define an array of holiday dates. This array can be hardcoded for a small, static set of holidays, or it can be populated dynamically by fetching data from an API or a database. For example, a robust system might integrate with a public holiday API to ensure its list is always up-to-date. Once you have your list, you can check each date against this list within your beforeShowDay function. This dual check—for weekends and holidays—provides a comprehensive solution for restricted date selection.

To seamlessly disable specific dates, like public holidays, in the jQuery UI Datepicker, you can extend the beforeShowDay function by maintaining an array of holiday dates. Within this function, you’ll first check if the current day is a weekend. If it’s a weekday, you then iterate through your holiday array, comparing the date being rendered with each holiday. If a match is found, the date is disabled by returning [false, ''], otherwise it remains enabled with [true, '']. This method allows for precise control over which dates are selectable, ensuring business rules for non-working days are strictly adhered to.

$(function() { var nationalHolidays = ["1/1/2024", "1/15/2024", "5/27/2024", "7/4/2024", "9/2/2024", "11/28/2024", "12/25/2024"]; // Example holidays function noWeekendsOrHolidays(date) { var day = date.getDay(); // Disable Saturday (6) and Sunday (0) if (day == 0 || day == 6) { return [false, '']; } // Check for national holidays var formattedDate = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear(); if ($.inArray(formattedDate, nationalHolidays) != -1) { return [false, '', 'Holiday!']; } return [true, '']; } $("datepickerWithHolidays").datepicker({ beforeShowDay: noWeekendsOrHolidays }); }); 

This approach combines the weekend disabling logic with a check for predefined holidays, offering a robust solution for your date selection needs. Remember to format your holiday dates consistently with how they are returned from date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear() for accurate comparison.

Advanced Scenarios and Best Practices for Datepicker Customization

Beyond simply disabling weekends and holidays, the jQuery UI Datepicker offers capabilities for more complex scheduling needs. Consider scenarios where you need to block out specific date ranges, enforce a minimum or maximum selection lead time, or even dynamically update holiday lists based on regional settings. These advanced customizations require thoughtful planning and robust implementation to maintain a smooth user experience and ensure data integrity.

< Question & Answer :
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 from the available choices. Can this be done? If so, how?

There is the beforeShowDay option, which takes a function to be called for each date, returning true if the date is allowed or false if it is not. From the docs:


beforeShowDay

The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable and 1 equal to a CSS class name(s) or ’’ for the default presentation. It is called for each day in the datepicker before is it displayed.

Display some national holidays in the datepicker.

$(".selector").datepicker({ beforeShowDay: nationalDays}) natDays = [ [1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'], [4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'], [7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'], [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke'] ]; function nationalDays(date) { for (i = 0; i < natDays.length; i++) { if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) { return [false, natDays[i][2] + '_day']; } } return [true, '']; } 

One built in function exists, called noWeekends, that prevents the selection of weekend days.

$(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends }) 

To combine the two, you could do something like (assuming the nationalDays function from above):

$(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays}) function noWeekendsOrHolidays(date) { var noWeekend = $.datepicker.noWeekends(date); if (noWeekend[0]) { return nationalDays(date); } else { return noWeekend; } } 

Update: Note that as of jQuery UI 1.8.19, the beforeShowDay option also accepts an optional third paremeter, a popup tooltip