Block Sundays and Specific Dates in jQuery UI datepicker

I am using the jquery ui datepicker and have it working just fine.

What I am trying to do is block Sundays and a list of specific dates. I have accomplished each task individually but I can’t figure out how to do both at the same time. (not sure how to combine the functions).

If you can offer assistance, it would be MUCH appreciated.

My code to blog specific dates

var disabledDays = ["2-20-2012"];

/* 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($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
      //console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
      return [false];
    }
  }
  //console.log('good:  ' + (m+1) + '-' + d + '-' + y);
  return [true];
}

function noHolidays(date) {
  return nationalDays(date);
}

	$(function() {
		$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd', minDate: +1, beforeShowDay: noHolidays });
	});

here is the code I use to just block Sundays (it’s inline with the datepicker function

$(function() {
		$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd', minDate: +1, beforeShowDay: function(date) {
        var day = date.getDay();
        return [(day != 0), ''];
    } });
	});

How can I accomplish both of these actions? Thanks in advance for any help

First we’ll modify the function so that it returns true or false, and not an array instead.
That will allow us to use the result from the nationalDays and the sundays function, from the noHolidays function.


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($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
      //console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
      return true;
    }
  }
  //console.log('good:  ' + (m+1) + '-' + d + '-' + y);
  return false;
}

function sundays(date) {
    var day = date.getDay();
    return (day === 0);
}

function noHolidays(date) {
  return [!sundays(date) && !nationalDays(date)];
}

Awesome, thanks for the help. Do you know of a way to allow for wildcards in some dates in the “disabledDays” list?

Basically, some days I want to block every year such as christmas, new years, 4th of july etc so I would want to be able to use a date like “7-4-*” or some other format if needed such as “7 4 *”.

Let me know if this is possible :slight_smile: Thanks again for the help!

One way could be something like this:


function isHoliday(date, holidays) {
    var parts,
        dateArray = [],
        i;
    for (i = 0; i < holidays.length; i += 1) {
        parts = holidays[i].split('/');
        if (parts.length === 2) {
            dateArray.push(new Date(date.getFullYear(), parts[0], parts[1] - 1).getTime());
        } else if (parts.length === 3) {
            dateArray.push(new Date(holidays[i]).getTime());
        } else {
            return false;
        }
    }
    return ($.inArray(date.getTime(), dateArray) !== -1);
}

function workDays(date) {
    var holidays = ['12/25', '1/1', '5/10/2011', '5/25'];
    return [!sundays(date) && !isHoliday(date, holidays)];
}