[LRUG] How does Rails decide how to parse ambiguous dates?

John Winters john at sinodun.org.uk
Tue Jul 26 03:39:51 PDT 2011


I'm working on a Rails application, using JQuery-UI to allow the user to 
enter a date.

I want to use a date format which my users will expect - specifically 
dd/mm/yyyy - and this is no problem to set in the JQuery-UI date picker.

However, I'm aware that there is potential for confusion with American 
date formats, where they put the month before the day.  I therefore have 
been trying to check exactly how Rails will interpret, say, "05/06/2011".

Rails seems to add a parse() method to the Date class, but as far as I 
can discover it isn't fully documented anywhere.   If I go into the 
Rails console in a brand new Rails (3.1) application (no localization 
configuration) and type:

d = Date.parse("5/6/2011")

then it reliably interprets it as the 5th of June (which is what I 
want), but I'm concerned that I don't know what is achieving this 
behaviour, and under what circumstances it would change.

I suppose I have two questions therefore:

1) How exactly does Rails interpret a date string typed into a field? 
What method does it use?

2) How does one control this behaviour?

Thanks for any assistance.

Cheers,
John



More information about the Chat mailing list