Advertisement
Advertisement


Set today's date as default date in jQuery UI datepicker


Question

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);
2016/04/09
1
69
4/9/2016 6:54:13 AM


You have to initialize the datepicker before calling a datepicker method

Here is a

Working JSFiddle.


$("#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" />


P.S: This assumes that you have the correct date set in your computer

2017/08/07

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

2018/12/03

$("#date").datepicker.regional[""].dateFormat = 'dd/mm/yy';
$("#date").datepicker("setDate", new Date());

Always work for me

2013/09/09

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());
2013/09/09

Note: When you pass setDate, you are calling a method which assumes the datepicker has already been initialized on that object.

$(function() {    
   $('#date').datepicker();
   $('#date').datepicker('setDate', '04/23/2014');
});

Test: http://jsfiddle.net/wimarbueno/hQkec/1/

2014/04/23

Source: https://stackoverflow.com/questions/4915990
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]