3
votes

I set a range of years like this

$(".date").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: '1900:c-18'
});

That shows a year dropdown from 1900 to "18 years before today". The datepicker it set to today's day but in 1900. How can I make the years dropdown to be set on the last available year?

FIDDLE: http://jsfiddle.net/fdE2S/

2

2 Answers

8
votes

If you carefully see the datepicker, you will find it is pointing to current date

03/09/2013 not 03/09/1990

Since you have hideout the current year it is not showing up. ie., if you remove the the yearRange you will find it.

Therefore you need to set the year explicitly in defaultDate like this

defaultDate : '-18y' //Subtract 18 years from current date

Finally the complete code

var date = new Date();
$(".date").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: '1900:-18',
    defaultDate : '-18y'    
});

Check this in JSFiddle

1
votes

Try this:

$(".date").datepicker({
    changeMonth: true,
    changeYear: true,
    defaultDate: "-18y",
    minDate: new Date(1900, 1, 1, 0, 0, 0, 0),
    maxDate: "-18y",
    yearRange: '1900:-18'
});

Use of Min and maxDate well restrict it so user cannot select beyond 18 years ago today. Also, using defaultDate will set the date as you requested, to 18 years ago today via the format "-18y".

jsFiddle