Stophface Stophface - 2 months ago 16
jQuery Question

Set min/max Date of jquery Datepicker

I am having trouble understanding how to set the min/max Date of the jquery Datepicker. When I check the docs, i see that the

dateformat
is expected in
new Date(2009, 1 - 1, 26)
.
What does the
1-1
mean? When I chec w3c schools for js dateformats, I cannot find this one.

So I tried

$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yyyy-mm',
minDate: new Date(2001-01),
maxDate: '+15Y'
});


But that leaves me with completely random results... It starts 2006 and ends 2026.

Here is fiddle
http://jsfiddle.net/ANF2y/58/

enter image description here

Answer

In JavaScript month is calculated from 0 to 11. So while creating a new Date use current month - 1.

1 - 1 equates to 0. So that is a valid month (0 - January).

In your case set minDate as below.

$("#datepicker").datepicker({
  changeMonth: true,
  changeYear: true,
  minDate: new Date(2001, 0, 1),
  maxDate: '+15Y',
  dateFormat: 'yyyy-mm'
});

that is because the jquery date picker only shows 15 items at a time, there is no max date set so you can go as far in the future as you want. When it is set to 2001 you see 2006 in the dropdown as it is limited to 15 items. Select 2006 and then you'll see 2001 in the dropdown. Is your question can I show more items in the year dropdown list?

Comments