2
votes

I have a jQuery datepicker. I want to set the default date to "current month, current day, (current year-18)" but I have issue when I set the defaultDate property. The default year displayed is 1920 which is supposedly 1995. Can anyone help?

My code:

HTML

<input type="text" class="datepicker minimumSize" name="BirthDate" id="BirthDate"  readonly="readonly"/>

JavaScript

var d = new Date();
var year = d.getFullYear() - 18;
$('#BirthDate').datepicker({ changeYear: true, changeMonth: true, yearRange: '1920:' + year + '', defaultDate: d.getMonth()+d.getDay()+year});

JSFiddle http://jsfiddle.net/jobzky/zg8Le/

3
Is someone making an 18+ site? - mpen

3 Answers

5
votes

Try

var d = new Date();
var year = d.getFullYear() - 18;
d.setFullYear(year);
$('#BirthDate').datepicker({ changeYear: true, changeMonth: true, yearRange: '1920:' + year + '', defaultDate: d});

Demo: Fiddle

1
votes

In your fiddle you have defaultYear which is not a valid option. Here you have defaultDate and by the way you're adding the day month and year together (also not likely what you mean) it seems you want to make it the date.

I suspect you want this instead:

$('#BirthDate').datepicker({ 
   changeYear: true, 
   changeMonth: true, 
   yearRange: '1920:' + year, 
   defaultDate: new Date(year,d.getMonth(),d.getDay())
});
0
votes

try this .This will solve your problem

var d = new Date();
var year = d.getFullYear() - 18 ;

$('#BirthDate').datepicker({ 
    changeYear: true,
    changeMonth: true,
    yearRange: '1920:' + year, 
   defaultDate: new Date(year, d.getMonth(), d.getDate())
   });