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

Jordi Noguera Leon jordinoguera83 at gmail.com
Tue Jul 26 07:53:19 PDT 2011


You can specify the formats for your dates in config/initializers/time.rb
with:

Date::DATE_FORMATS[:default] = "%d/%m/%y"
Date::DATE_FORMATS[:whatever] = "%y-%m-%d"

(Date::DATE_FORMATS or Time::DATE_FORMATS depending on whether you're
dealing with Date objects or Time objects)

Then, @date.to_s should do what you're expecting and you could do
@date.to_s(:whatever)


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

> On 26/07/11 14:48, Paul Robinson wrote:
>
>> On 26 Jul 2011, at 14:20, John Winters wrote:
>>
>>  You make reference to "helpers" - can you provide any pointers to
>>> those? I keep find myself going round and round in the documentation
>>> in the area of Date/Time/DateTime, with one bit referring to another
>>> bit, which refers you to a third bit and then back to the start, with
>>> none of them providing the hard information.
>>>
>>
>>
>> Oh Lord, this is actually a bit of a mess for complex cases, but if your
>> use case is simple, it's dead easy.
>>
>> You might want to get familiar with i18n:
>>
>> http://guides.rubyonrails.org/**i18n.html<http://guides.rubyonrails.org/i18n.html>
>>
>
> [snip]
>
> I've taken a look at that, but I *think* it's heading off in a different
> direction from what I'm trying to achieve, and from searching other queries
> found on the web it appears it doesn't even achieve what I want.
>
> That covers how to get different date formats to appear for different
> users, *in the body of your pages*.  What it doesn't appear to do is cover
> how to get different date formats to appear by default in your fields.  I've
> set up a skeletal test and whilst I can get:
>
> my_date.to_s
>
> to produce different strings depending on the format which I set up, a text
> field still staunchly displays:
>
> 2011-07-26
>
> regardless of any formats specified.
>
> I'm currently digging through the Rails source (which is hard on the eyes)
> trying to find out exactly what conversion the field helpers are using, and
> whether it can be changed by configuration options.
>
> Eventually I expect I'll get bored and just use the sledgehammer approach
> which I started with, but I really hate that kind of messy solution.
>
>
> 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/e331102b/attachment-0003.html>


More information about the Chat mailing list