jQuery UI Datepicker and Timepicker conflict with minDate and maxDate (Error parsing the date string: Missing number at position 0)

StackOverflow https://stackoverflow.com/questions/14706844

Question

I am using Rails 3.2.11 and am trying to use both jQuery UI Datepicker and Timepicker, however I am facing problems when trying to use the minDate and maxDate properties of Datepicker.

My view for one page (in HAML), looks like this:

.field_section
  = f.label :date, "Date"
  %br
  = f.datepicker :date, :minDate => "-10y", :maxDate => "+1y", :dateFormat => "yy-mm-dd",
    :constrainInput => true, :showOtherMonths => true, :size => 10

I have jQuery UI 1.10 and everything works perfectly.

However, in a completely separate view, i also need a timepicker, so i include the jquery-ui-timepicker-addon.js file (version 1.20) in my application.js

The order of inclusion is:

  1. jQuery (1.8.3)
  2. jQuery UJS jQuery UI (specifically, autocomplete, datepicker and slider)
  3. Timepicker

Now, loading the first view (which only has a datepicker, not timepicker) gives me the following error in Chrome JS console, when i click on the datepicker input:

Error parsing the date string: Missing number at position 0
date string = -10y
date format = yy-mm-dd
Error parsing the date string: Missing number at position 0
date string = +1y
date format = yy-mm-dd 

Which is repeated another 5 or so times. The errors point to jquery-ui-timepicker-addon.js:1912, which, unhelpfully is just:

$.timepicker.log = function(err){
    if(window.console)
        console.log(err);
};

Oddly, if i remove the minDate and maxDate attributes from my view, the errors go away - but i want to use these options. Any idea what is going on?

Thanks

Update: i have noticed if i pass minDate and maxDate as hard coded strings, ie

:minDate => "2000-01-01", :maxDate => "2014-01-01"

the errors go away. I assume this means there is an error with dateFormat (somewhere..?)

Was it helpful?

Solution

So it looks like Timepicker does not understand relative dates. I think Timepicker attempts to always override Datepicker. With :minDate and :maxDate set to relative values (ie, +1y) Timepicker sees a format mismatch between +1y and yy-mm-dd for example - it does not seem to first convert the +1y to a Date object with format yy-mm-dd.

The solution for me was to explicity put in Date objects:

:minDate => Date.today - 10.years, :maxDate => Date.today + 1.years

Which really isn't as nice as +1y

Issue raised on Github

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