72

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);
T J
  • 40,740
  • 11
  • 73
  • 131
Chris Abrams
  • 32,712
  • 19
  • 49
  • 56

13 Answers13

114

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.

Martin Eden
  • 5,785
  • 3
  • 26
  • 33
89

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

Hassan Imam
  • 16,414
  • 3
  • 29
  • 41
T J
  • 40,740
  • 11
  • 73
  • 131
  • 1
    Thanks! you could also add to your example datepicker("selectedDate", new Date()); which is selecting the date only when the OP opens the calendar. – Revious Nov 12 '14 at 21:39
  • @Revious Actually, I couldn't find any reference to such method... There's also [`defaultDate`](http://api.jqueryui.com/datepicker/#option-defaultDate), but it won't set the date inside the textbox which is being asked in question... – T J Nov 16 '14 at 09:36
33

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

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

Always work for me

Ashisha Nautiyal
  • 1,369
  • 2
  • 19
  • 39
4

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());
Chintan Khetiya
  • 15,208
  • 9
  • 41
  • 81
Usman Younas
  • 1,203
  • 13
  • 18
4

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/

Wilzon
  • 121
  • 4
4

Set default date in initialization:

$("#date").datepicker({
    defaultDate: '01/26/2014'
});
Morten Holmgaard
  • 6,452
  • 6
  • 55
  • 79
2

This worked for me and also localised it:

$.datepicker.setDefaults( $.datepicker.regional[ "fr" ] );
$(function() {
   $( "#txtDespatchDate" ).datepicker( );
   $( "#txtDespatchDate" ).datepicker( "option", "dateFormat", "dd/mm/yy" );
   $('#txtDespatchDate').datepicker('setDate', new Date());
});
T J
  • 40,740
  • 11
  • 73
  • 131
Jan
  • 41
  • 1
1

Just set minDate: 0

Here is my script:

$("#valid_from")
   .datepicker({
     minDate: 0,
     defaultDate: "+1w",
     changeMonth: true,
     numberOfMonths: 3
})
Mike Nguyen
  • 987
  • 1
  • 10
  • 15
1

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

"mydate" is the ID of input or datepicker element/control.

        $(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

You have not defined

$("#mydate").datepicker({});
T J
  • 40,740
  • 11
  • 73
  • 131
Ashish Mehta
  • 6,443
  • 4
  • 22
  • 48
0

Try this:

$('.datepicker').datepicker('update', new Date(year,month,day));
Gautam Savaliya
  • 1,209
  • 2
  • 17
  • 28
-1

try this:

$("#mydate").datepicker("setDate",'1d');
Ethan More
  • 11
  • 1