I just want today's date to be the default value in the input that is using jQuery UI's datepicker
:
<input id="mydate" type="text" />
I tried the below code but it didn't work:
var currentDate = new Date(); $("#mydate").datepicker("setDate",currentDate);
Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );
inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });
datepicker. formatDate("dd-mm-yy", new Date())); Add the above code at the end of the script.
If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.
You need to make the call to setDate separately from the initialization call. So to create the datepicker and set the date in one go:
$("#mydate").datepicker().datepicker("setDate", new Date());
Why it is like this I do not know. If anyone did, that would be interesting information.
You have to initialize the datepicker before calling a datepicker method.
$("#mydate").datepicker().datepicker("setDate", new Date()); //-initialization--^ ^-- method invokation
<link href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <input type="text" id="mydate" />
Here is a Working JSFiddle.
P.S: This assumes that you have the correct date set in your computer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With