3

I have a pretty simple code involving the jQuery UI datepicker:

$('.date-picker').datepicker({
      dateFormat: 'MM yy',
      changeMonth: true,
      changeYear: true,
      showButtonPanel: true,
 })
 .change(function() {
      var date = $(this).datepicker('getDate');
      $('#dateShow').val(date);
 });

The problem is that in the change handler I always get the today date, not the one I've picked in the calendar, every time I select another month or year. Here is jsFiddle.

Does anybody have an idea of what I'm doing wrong? How could such a basic use case lead datepicker's getDate to return an incorrect value?

UPDATE: The code sample is simplified and question is reformulated accordingly.

1 Answer 1

7

There seems to be an existing bugs for this issue Bug 8009, Bug 8159

It doesn't seems like its fixed in the new version. The best way to get around this issue is to use $(this).val() instead of $(this).datepicker('getDate').

Here's a working example : http://jsfiddle.net/qk5qj33u/9/

Sign up to request clarification or add additional context in comments.

9 Comments

If I'm understanding correctly, those issues are all about when you set an invalid date or even null via the setDate. As opposed, it's always correct in my case. Or I'm missing something?
For Bug 8159, yes but Bug 8009 is the same issue as yours.
8009: Datepicker: getDate returns the incorrect date **when typing invalid value**. My: $(this).datepicker('setDate', new Date(year, month, 1)).change();. Could you please point to the match with my case?
It has nothing to do with SetDate. It works fine. It's about when you try to extract the date via "getDate". This is the text from that bug : I'm using the ​datepicker demo. When I input "09012013" into the input field (and then click on some whitespace to close the calendar window), it sets the date to today, according to getDate. Shouldn't getDate return Sep 12 2013?
I've updated the jsFiddle. In this version, I don't use the setDate and onClose at all. When I'm selecting a new date by clicking on a day in the calendar, getDate returns the date I've just selected since the second try on, though, At the first time it returns today regardless of the selection.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.