This option will reset the home page of this site. Restoring any closed widgets or categories.

Reset

jQuery UI DatePicker: Disable Specified Days

jQuery Calendar Picker

One project I’m currently working on requires jQuery. The project also features a datepicker for requesting a visit to their location. jQuery UI’s DatePicker plugin was the natural choice and it does a really nice job. One challenge I encountered was the need to prevent specific days from being picked. Here’s the jQuery javascript I used to accomplish that.

The jQuery Javascript

/* create an array of days which need to be disabled */
var disabledDays = ["2-21-2010","2-24-2010","2-27-2010","2-28-2010","3-3-2010","3-17-2010","4-2-2010","4-3-2010","4-4-2010","4-5-2010"];

/* utility functions */
function nationalDays(date) {
	var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
	//console.log('Checking (raw): ' + m + '-' + d + '-' + y);
	for (i = 0; i < disabledDays.length; i++) {
		if(ArrayContains(disabledDays,(m+1) + '-' + d + '-' + y) || new Date() > date) {
			//console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
			return [false];
		}
	}
	//console.log('good:  ' + (m+1) + '-' + d + '-' + y);
	return [true];
}
function noWeekendsOrHolidays(date) {
	var noWeekend = jQuery.datepicker.noWeekends(date);
	return noWeekend[0] ? nationalDays(date) : noWeekend;
}

/* taken from mootools */
function ArrayIndexOf(array,item,from){
	var len = array.length;
	for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
		if (array[i] === item) return i;
	}
	return -1;
}
/* taken from mootools */
function ArrayContains(array,item,from){
	return ArrayIndexOf(array,item,from) != -1;
}

/* create datepicker */
jQuery(document).ready(function() {
	jQuery('#date').datepicker({
		minDate: new Date(2010, 0, 1),
		maxDate: new Date(2010, 5, 31),
		dateFormat: 'DD, MM, d, yy',
		constrainInput: true,
		beforeShowDay: noWeekendsOrHolidays
	});
});

The base code is taken from this forum post. You’ll note that I created an array of dates in string format which also accommodates for comparing year.

I’d like to see jQuery UI implement a standard way of disabling days. Their DatePicker is very nice though so I can’t complain too much!

Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!

Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!

jQuery UI DatePicker: Disable Specified Days

Related posts:

  1. PHP Function – Calculating Days In A Month
  2. dwImageProtector Plugin for jQuery
  3. Using Opacity to Show Focus with jQuery
  4. Adding Days To Dates In MySQL
  5. Disable Right Click Using MooTools 1.2

View full post on David Walsh :: PHP, CSS, MooTools, jQuery, and Everything Else

No related posts.

7 Comments

  1. Mark

    I have to admit, I wish Mootools came with such a wide variaty of UI plugins…

    All and all, I think you accomplished your goal very well! Good stuff.

  2. Salih Gedik

    Nice. But I rarely need jQ. Mootools FTW isn’t it?

  3. Simeon

    Nice post! Can be easily expanded by populating he “disabledDays” using PHP to find all weekends, holidays, etc. I wonder what the performance impact is though if you span across several years as a typical selection might allow?

  4. Savageman

    @mark: you should try this Mootools DatePicker (also works for time): http://www.monkeyphysics.com/mootools/script/2/datepicker

    Doesn’t have a beforeShowDay option though…

  5. Douglas Neiner

    I would remove all the MooTools helpers :) and just use the jQuery native $.inArray function:

    Change this line: ArrayContains(disabledDays,(m+1) + ‘-’ + d + ‘-’ + y) to this instead: $.inArray(disabledDays,(m+1) + ‘-’ + d + ‘-’ + y) != -1

    Then you don’t need to two other functions from MooTools.

  6. Douglas Neiner

    @Douglas Neiner: Sorry, flip the arguments. $.inArray( value, array )

  7. David Walsh

    @Douglas Neiner: I’m a complete MooTools nerd — I didn’t even think to check the jQuery API. Will update.