How to disable different months in each of the ranges of the date picker using the beforeShowDay method

I would like to disable different months in each of the date picker years, in particular, at the beginning and at the end of the year. I know that I can use the beforeShowDay method to run some code that can return true and false to enable / disable dates. Now I want to turn off the months in September - December 2005 and in November + December in 2009, but I'm not quite sure how to do this.

When I date.getYear() out of date.getYear() , I get numbers like 104 105, when I expect to get 2014 and 2015, etc., so I'm not sure how I can check the year, then switch and set the values ​​to true / false based on the minimum and maximum arrays that I created that will contain the months that I want to exclude.

Js

 var minYear = 2005, maxYear = 2009, excludeMinYearMonths = [8, 9, 10, 11], excludeMaxYearMonths = [10, 11]; $("#datepicker").datepicker({ changeMonth: true, changeYear: true, yearRange: minYear + ':' + maxYear, beforeShowDay: function (date) { console.log(date.getYear()); if (date.getYear() === minYear) { if ($.inArray(date.getMonth(), excludeMinYearMonths) > -1) { return [false, '']; } return [true, '']; } if (date.getYear() === maxYear) { if ($.inArray(date.getMonth(), excludeMaxYearMonths) > -1) { return [false, '']; } return [true, '']; } }, defaultDate: '01/01/05' }); 

Jsfiddle: http://jsfiddle.net/qA9NT/18/

0
source share
1 answer

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

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

In addition, you forgot to define the default behavior for 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 out the updated live example.

+1
source

Source: https://habr.com/ru/post/1232964/


All Articles