Set today's date as default date in jQuery UI datepicker [duplicate]
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);
Solution 1:
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.
Solution 2:
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
Solution 3:
Its very simple you just add this script,
$("#mydate").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", new Date());
Here, setDate set today date & dateFormat define which format you want set or show.
Hope its simple script work..
Solution 4:
$("#date").datepicker.regional[""].dateFormat = 'dd/mm/yy';
$("#date").datepicker("setDate", new Date());
Always work for me
Solution 5:
This one work for me , first you bind datepicker to text-box and in next line set (today as default date) the date to it
$("#date").datepicker();
$("#date").datepicker("setDate", new Date());