Search code examples
jqueryjquery-uijquery-ui-datepicker

Jquery DatePicker Set default date


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.


Solution

  • 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