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:
- jQuery (1.8.3)
- jQuery UJS jQuery UI (specifically, autocomplete, datepicker and slider)
- 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..?)
So it looks like Timepicker does not understand relative dates. I think Timepicker attempts to always override Datepicker. With
:minDateand:maxDateset to relative values (ie,+1y) Timepicker sees a format mismatch between+1yandyy-mm-ddfor example – it does not seem to first convert the+1yto a Date object with formatyy-mm-dd.The solution for me was to explicity put in Date objects:
Which really isn’t as nice as
+1yIssue raised on Github