Search code examples
jqueryjquery-uijquery-ui-datepicker

Open jQueryUI DatePicker on current month, regardless of what date is selected


The DatePicker will always open on the date which is selected in the input. I need to open DatePicker at the current month, no matter what date is selected in the input.

I have tried the following, but it will change the value of the input as well, which is not desirable.

beforeShow: function() {
  $(input).datepicker("setDate", "0");
}

Solution

  • After playing with it for a while I did it this way:

    $('input').datepicker({
        beforeShow: function(input, inst) {
            setTimeout(function() {  
                var date = new Date();
                inst.selectedDay = date.getDate();
                inst.drawMonth = inst.selectedMonth = date.getMonth();
                inst.drawYear = inst.selectedYear = date.getFullYear();
                $.datepicker._adjustDate(input);
            }, 1);
        }
    });
    

    https://jsfiddle.net/ouw8p9gc/