Skip to content Skip to sidebar Skip to footer

How To Disable Different Months In Each Of The Datepicker Range Years Using Beforeshowday Method

I would like to disable different months in each of of datepicker years specifically the start year and the end year. I know I can use the beforeShowDay method to run some code tha

Solution 1:

You can start by reading a documentation of getYear(). There you will see why getFullYear() is used instead.

The getYear() method returns the year minus 1900 in the specified date according to local time. Because getYear() does not return full years, it is no longer used and has been replaced by the getFullYear() method.

Also, you forgot to define a default behavior for the non-excluded dates.

var minYear = 2005, maxYear = 2009, excludedMonths = {
    2005: [8, 9, 10, 11],
    2009: [10, 11]
};

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: minYear + ':' + maxYear,
    beforeShowDay: function (date) {
        if (excludedMonths.hasOwnProperty(date.getFullYear())) {
            if ($.inArray(date.getMonth(), excludedMonths[date.getFullYear()]) > -1) {
                return [false, ''];
            }
        }
        return [true, ''];
    },
    defaultDate: '01/01/05'
});

Check the updated live example.

Post a Comment for "How To Disable Different Months In Each Of The Datepicker Range Years Using Beforeshowday Method"