Jquery datepicker year range default

Having a bit of a problem with the jQuery datePicker , I suspect it's just a tuning issue that I missed or made a mistake.

If you look at this simple script: JS Fiddle

You will see that I set the range of the year, so that by default, when you click on input, it will open it in 1994, but if you then click on any of these dates, for example. September 3, in fact, it will be placed at the entrance in 2012, and not in the year that is selected in the drop-down menu.

How can I make it so that it uses the correct year without changing the drop-down list and then changing it again?

Greetings.

+6
source share
3 answers

Strange, but adding defaultDate:"1994-01-01" to the datepicker parameters seems to fix it.

Fiddle

+5
source

Like others, you must set defaultDate. However, unlike other solutions, you do not want to specify a date, since your drop-down list will be updated as the years pass, as you make a relative list.

Try this instead ...

 function loadDatePicker() { $('.datePicker').datepicker({ dateFormat: 'yy-mm-dd', changeYear: true, changeMonth: true, yearRange: "-18:-12", defaultDate:"-18y-md" // Relative year/month/day }); } 
+7
source

I think I have a reason .. This is because yearRange: just limit the range only in the dropdown menu. He just changes the list with nothing else.

They are a mansion here.

http://jqueryui.com/demos/datepicker/#option-yearRange [Note that this option only affects what appears in the drop-down list to limit which dates can be selected using the minDate and / or maxDate.]

To verify this, set the date selector as shown below and you will find all dates that have been disabled. because he saws us the calender of 2012, not 1994

 $('.datePicker').datepicker({ dateFormat: 'yy-mm-dd', changeYear: true, changeMonth: true, yearRange: "-18:-12", maxDate: new Date(2012,1-1,1) }); 

To fix this, you should use defaultDate as well as minDate and maxDate

I hope I understand.

0
source

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


All Articles