Set date today as default date in jQuery UI datepicker

I just want today's date to be the default value on input using jQuery UI datepicker :

 <input id="mydate" type="text" /> 

I tried the code below, but it did not work:

 var currentDate = new Date(); $("#mydate").datepicker("setDate",currentDate); 
+57
jquery jquery-ui jquery-ui-datepicker
Feb 06 2018-11-11T00:
source share
13 answers

You need to make the setDate call separate from the initialization call. So, to create a datepicker and set the date in one go:

 $("#mydate").datepicker().datepicker("setDate", new Date()); 

Why so I do not know. If someone did this, that would be interesting information.

+80
Jul 05 '13 at 15:35
source share

You must initialize the datepicker before calling the datepicker method

Here

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" /> 



PS: This assumes that you have the correct date on your computer

+70
Nov 12 '14 at 19:19
source share

Very simple, you just added this script,

 $("#mydate").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", new Date()); 

Here setDate is set today to date & dateFormat, which defines the format you want to set or show.

Hope its a simple script work.

+18
Mar 18 '14 at 8:32
source share
 $("#date").datepicker.regional[""].dateFormat = 'dd/mm/yy'; $("#date").datepicker("setDate", new Date()); 

Always work with me

+5
Sep 09 '13 at 4:26
source share

Note. When you pass setDate, you call a method that assumes that datepicker is already initialized on this object.

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

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

+3
Apr 23 '14 at 17:52
source share

This works for me, first you bind the datepicker to the text box and in the next set of lines (today as the default date) the date for it

 $("#date").datepicker(); $("#date").datepicker("setDate", new Date()); 
+2
Jul 13 '13 at 19:20
source share

Set the default initialization date:

 $("#date").datepicker({ defaultDate: '01/26/2014' }); 
+2
Sep 03 '14 at 16:27
source share

This worked for me and also localized it:

 $.datepicker.setDefaults( $.datepicker.regional[ "fr" ] ); $(function() { $( "#txtDespatchDate" ).datepicker( ); $( "#txtDespatchDate" ).datepicker( "option", "dateFormat", "dd/mm/yy" ); $('#txtDespatchDate').datepicker('setDate', new Date()); }); 
+1
Jul 31 '14 at 2:28
source share

Just set minDate: 0

Here is my script:

 $("#valid_from") .datepicker({ minDate: 0, defaultDate: "+1w", changeMonth: true, numberOfMonths: 3 }) 
+1
Oct 12 '16 at 9:27
source share

You did not identify

 $("#mydate").datepicker({}); 
0
Oct 24
source share

Try the following:

 $('.datepicker').datepicker('update', new Date(year,month,day)); 
0
Jun 09 '16 at 16:28
source share

I tried many ways and came up with my own solution that works absolutely as required.

"mydate" is the identifier of an input / date item or item.

  $(document).ready(function () { var dateNewFormat, onlyDate, today = new Date(); dateNewFormat = today.getFullYear() + '-' + (today.getMonth() + 1); onlyDate = today.getDate(); if (onlyDate.toString().length == 2) { dateNewFormat += '-' + onlyDate; } else { dateNewFormat += '-0' + onlyDate; } $('#mydate').val(dateNewFormat); }); 
0
Dec 08 '16 at
source share

try the following:

 $("#mydate").datepicker("setDate",'1d'); 
-one
Jul 23. '14 at 3:08
source share



All Articles