Jquery DatePicker Set default date

maaz picture maaz · Jan 29, 2013 · Viewed 253.1k times · Source

I Have two date fields where I use DatePicker to pick the dates.

For the first date field, I want today's date as the default date.
For the second date field, I need today + 15 days as my default date

jQuery

$("[name=trainingStartFromDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true});
$("[name=trainingStartToDate]").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true}); 

How do I set the default dates?

I have tried setDate: new Date() for first date but it's not working.

Answer

gdoron is supporting Monica picture gdoron is supporting Monica · Jan 29, 2013

Today date:

$( ".selector" ).datepicker( "setDate", new Date());
// Or on the init
$( ".selector" ).datepicker({ defaultDate: new Date() });

15 days from today:

$( ".selector" ).datepicker( "setDate", 15);
// Or on the init
$( ".selector" ).datepicker({ defaultDate: 15 });

jQuery ui docs