Reputation: 1286
I have two text boxes , I want dates to be populated as 6 months back and 15 days ahead date from current date. I am able to select default date in datepicker , but not able to set the default date to text box
$("#fromDate").datepicker({ dateFormat: 'yy-mm-dd',defaultDate : '-6m' });
$("#toDate").datepicker({ dateFormat: 'yy-mm-dd',defaultDate: '+15d' });
Please find fiddle here : datepickerIssue
Thanks
Upvotes: 4
Views: 5698
Reputation: 2794
Firstly, you should call datepicker() and then call datepicker again to setDate with new Date().
$( document ).ready(function() {
$("#datepicker1").datepicker().datepicker("setDate", new Date());
$("#datepicker2").datepicker().datepicker("setDate", new Date());
})
In other case where you know what date you want to set as default date, you can place it in value of input element.
<input type="text" id="datepicker2" value='21/01/2014'>
Upvotes: 0
Reputation: 103335
You need to chain the setDate
method call after your datepicker initialization and then use the usual relative notation of datepicker, for example to get the following 15 days or previous 6 months:
$("#datepicker1").datepicker({ dateFormat: "yy-mm-dd" }).datepicker("setDate", "-6m");
$("#datepicker2").datepicker({ dateFormat: "yy-mm-dd" }).datepicker("setDate", "+15d");
Upvotes: 8