4

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

1
  • you want to have the date already in the input tag before selecting the date? Commented Jan 23, 2015 at 15:04

2 Answers 2

8

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");

JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

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'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.