133

How can I change the selected date of jquery Date picker dynamically on the fly? I have say created a inline date picker. Then after some time, I want to reflect a different date there without recreating the datepicker from the scratch.

I tried the setDate method, but did not work, and there is not much documentation in the doc.

There is another (extended?) plugin here, but I want to use the plugin which is shipped with jquery.ui.all.js.

Sabya
  • 11,534
  • 17
  • 67
  • 94

14 Answers14

208

What version of jQuery-UI are you using? I've tested the following with 1.6r6, 1.7 and 1.7.1 and it works:

//Set DatePicker to October 3, 2008
$('#dateselector').datepicker("setDate", new Date(2008,9,03) );
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
Ben Koehler
  • 8,633
  • 3
  • 23
  • 23
28

Check that the date you are trying to set it to lies within the allowed date range if the minDate or maxDate options are set.

cfwall
  • 431
  • 4
  • 8
12

This example shows of how to use default value in the dropdown calendar and value in the text box. It also shows how to set default value to previous date.

selectedDate is another variable that holds current selected date of the calendar control

    var date = new Date();
    date.setDate(date.getDate() - 1);

    $("#datepicker").datepicker({
        dateFormat: "yy-mm-dd",
        defaultDate: date,
        onSelect: function () {
            selectedDate = $.datepicker.formatDate("yy-mm-dd", $(this).datepicker('getDate'));
        }
    });

    $("#datepicker").datepicker("setDate", date);
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
8

Noted that for DatePicker by Keith Wood (http://keith-wood.name/datepickRef.html) the following works - note that the setting of the default date is last:

    $('#datepicker').datepick({
        minDate: 0,
        maxDate: '+145D',
        multiSelect: 7,
        renderer: $.datepick.themeRollerRenderer,
        ***defaultDate: new Date('1 January 2008')***
    });
Michael
  • 81
  • 1
  • 1
8
$('.date-pick').datePicker().val(new Date()).trigger('change')

finally, that what i look for the last few hours! I need initiate changes, not just setup date in text field!

Mohammad
  • 21,175
  • 15
  • 55
  • 84
7

For some reason, in some cases I couldn't make the setDate work.

A workaround I found is to simply update the value attribute of the given input. Of course the datepicker itself won't be updated but if what you just look for is to display the date, it works fine.

var date = new Date(2008,9,3);
$("#your-input").val(date.getMonth()+"/"+date.getDate()+"/"+date.getFullYear());
// Will display 9/3/2008 in your #your-input input
Guillaume Flandre
  • 8,936
  • 8
  • 46
  • 54
5

This is a old thread but I just want to give some information how I have used. If you want to set today date you can simply apply it like below.

 $("#datepickerid").datepicker("setDate", "0");

If you want to set some days before/after the current date then use the symbol -/+ symbol of how many days you want like below.

 $("#datepickerid").datepicker("setDate", "-7");
 $("#datepickerid").datepicker("setDate", "+5");
Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54
4

please Find below one it helps me a lot to set data function

$('#datepicker').datepicker({dateFormat: 'yy-mm-dd'}).datepicker('setDate', '2010-07-25');

Shyam
  • 41
  • 1
  • I tried every solution on this page (and many more), and nothing worked for me except this one! Thank you! .... NOTE: you can use the following to add a single day to a given date... ```$('#datepicker').datepicker({dateFormat: 'yy-mm-dd'}).datepicker('setDate', new Date(...).getDate() + 2);``` – greenhouse Sep 05 '22 at 03:15
3

This works for me:

$("#dateselector").datepicker("option", "defaultDate", new Date(2008,9,3));
Gjermund Dahl
  • 1,410
  • 17
  • 25
2

In Html you can add your input field with a date picker like this

<input class="form-control date-picker fromDates" id="myDate" type="text">

and then in java script, initialize your datepicker and set your value to the date like this,

$('.fromDates').datepicker({
                maxDate: '0',
                dateFormat: "dd/mm/yy",
                changeYear: true,
                changeMonth: true,
                yearRange: "-100:+0"
            });

var myDateVal = moment('${value}').format('DD/MM/YYYY');
$('#myDate').datepicker().datepicker('setDate', myDateVal );

(In here fromdate attribute shows the previous dates of the current date)

chamzz.dot
  • 607
  • 2
  • 12
  • 24
2

setDate only seems to be an issue with an inline datepicker used in jquery UI, the specific error is InternalError: too much recursion.

Allen
  • 21
  • 1
0
var dt = new Date();
var renewal = moment(dt).add(1,'year').format('YYYY-MM-DD');
// moment().add(number, period)
// moment().subtract(number, period)
// period : year, days, hours, months, week...
study4u
  • 11
  • 2
  • Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what the code does and how it answers the question, so that it is useful for other users also as well as the OP. – FluffyKitten Jul 31 '20 at 07:16
-1

or you can simply have

$('.date-pick').datePicker().val(new Date()).trigger('change')
Gayan
  • 2,750
  • 5
  • 47
  • 88
-1

I had a lot of trouble with the setDate method as well. seems to only work in v1. What does seem to work however is using the dpSetSelected method:

    $("#dateselector").dpSetSelected(new Date(2010, 0, 26).asString());

good luck!

tijs
  • 797
  • 7
  • 24