Exclude Weekends or Holidays from Calendar Widget in Forms

Some organizations using a calendar widget on a form to capture a date input may want to restrict the selection of days on the calendar. The following code is provided to restrict the selection of weekends and holidays (a list that you can modify and maintain), and this code can be modified to restrict selection of other days of the week, too.

In your form, first ensure that your calendar field has an export label, such as "date". Then, click Edit Scripts and add the following code to the custom JavaScript:

var holidays = ['7/4/2016', '12/25/2016'];
form.getQuestion('date').find('.hasDatepicker').datepicker('option', { beforeShowDay: function(date)
{
var show = true;
if(date.getDay() == 0 || date.getDay() == 6){show = false;} //exclude weekends
for (var i = 0; i < holidays.length; i++) if (new Date(holidays[i]).toString() == date.toString()) { show = false; break; } //exclude holidays\\
return [show];
}});

Once the code has been added, test the form to ensure that the calendar days are being disabled as desired.

Exampleinline-512538150.png

You only need to reference your export key at the beginning, where it says form.getQuestion( . All other references to "date" should remain unchanged:

var holidays = ['7/5/2021', '12/25/2021'];
form.getQuestion('sys:field:callback').find('.hasDatepicker').datepicker('option', { beforeShowDay: function(date)
{
 var show = true;
 if(date.getDay() == 0 || date.getDay() == 6){show = false;} //exclude weekends
 for (var i = 0; i < holidays.length; i++) if (new Date(holidays[i]).toString() == date.toString()) { show = false; break; } //exclude holidays
 return [show];
}});
Was this article helpful?
9 out of 10 found this helpful