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

Matthew Rudy Jacobs matthewrudyjacobs at gmail.com
Tue Jul 26 09:25:06 PDT 2011


I've read some of the responses to this
but I'm going to say "why do you need to stick with this format?"

in particular
some browsers have started to implement the input[type=date] field.
and they implement the "yyyy-mm-dd" format.

What's good about this is that it is unambigous.

Here is my implementation;

   def html5_date_select(field, value)

  text_field_tag(field, value.to_s(:db), :type => "date")

end


Then I use Modernizr to detect their date support


// create a "rails" region, just to set the date format

jQuery(function($){

$.datepicker.regional['rails'] = {

dateFormat: 'yy-mm-dd',

};

$.datepicker.setDefaults($.datepicker.regional['rails']);

});


$(document).ready(function() {

  if(!Modernizr.inputtypes.date) {

    $("input[type=date]").datepicker();

  }

});




On 26 July 2011 11:39, John Winters <john at sinodun.org.uk> wrote:

> 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
> ______________________________**_________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/**listinfo.cgi/chat-lrug.org<http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110726/bba270be/attachment-0003.html>


More information about the Chat mailing list