Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
like image 948
Chris Abrams Avatar asked Feb 06 '11 20:02

Chris Abrams


People also ask

How do I change the default date in datepicker?

Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );

How can change date format in jQuery UI datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });

How can set current date in textbox using jQuery?

datepicker. formatDate("dd-mm-yy", new Date())); Add the above code at the end of the script.

What is minDate and maxDate in jQuery datepicker?

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.


2 Answers

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.

like image 62
Martin Eden Avatar answered Sep 21 '22 15:09

Martin Eden


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

like image 42
T J Avatar answered Sep 21 '22 15:09

T J