Question

I'm using jQuery's date-picker and specifying a minimum date of today and maximum date of Jan 31st. When the calendar shows up, however, the 31st cannot be selected, despite the code looking correct.

Here's the code:

function initializePickers() {
  jQuery('.date-start').each(function() {
      startEndPickers(this);
  });
  jQuery('.date-end').each(function() {
      startEndPickers(this);
  });
}

function startEndPickers(input) {
  jQuery(input).datepicker({
      dateFormat: "M dd, yy",
      minDate: new Date(),
      maxDate: new Date('2014-01-31')
  });
}

And here is a jsFiddle which demonstrates the inability to pick January 31st: http://jsfiddle.net/Hec5m/

Anyone know what's causing this? The maxDate is clearly specified as the 31st, not the 30th.

Was it helpful?

Solution

new Date('2014-01-31') chooses midnight on January 31, which means no time during the day will be able to be selected, which is why it is excluded.

You can fix this either by using 2014-02-01 as the max date (jsFiddle) or by specifying a time along with the end date at 23:59:59 (jsFiddle).

jQuery UI also allows you to put in a string as an argument (the same way as your date format--so "Jan 31, 2014" in your case), rather than passing in a Date object, which behaves as you would expect. (jsFiddle)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top