Forum: Ruby on Rails Custom date format

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9c0eebc506c56c64dc616cdf123fa671?d=identicon&s=25 Chris Chris (aboulafia)
on 2006-03-23 18:10
Hi, I've followed these directions :
http://railswiki.pdxruby.org/HowToDefineYourOwnDat...

It seems pretty straightforward but obviously it doesn't work for me...
article.date_edited.to_formatted_s(:my_format_1)

produce a default formatted date, not the one I've defined in
environment.rb :
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :my_format_1 => '%d/%m/%Y - %H:%M',
  :my_format_2  => '%l:%M %p, %B %d, %Y'
)

What's wrong ?

Thanks
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-23 20:56
Christophe Gimenez wrote:
> Hi, I've followed these directions :
> http://railswiki.pdxruby.org/HowToDefineYourOwnDat...
>
> It seems pretty straightforward but obviously it doesn't work for me...
> article.date_edited.to_formatted_s(:my_format_1)
>
> produce a default formatted date, not the one I've defined in environment.rb :
> ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
>   :my_format_1 => '%d/%m/%Y - %H:%M',
>   :my_format_2  => '%l:%M %p, %B %d, %Y'
> )

What does "it doesn't work for me" mean?

I'm doing this and it works fine. Just make sure the format definitions
in your environment.rb go outside the run block.

By the way, you can just use some_date.to_s(:format) - Date#to_s is
aliased to Date#to_formatted_s

--josh
http:blog.hasmanythrough.com
9c0eebc506c56c64dc616cdf123fa671?d=identicon&s=25 Chris Chris (aboulafia)
on 2006-03-23 21:11
Josh Susser wrote:
> Christophe Gimenez wrote:
>> Hi, I've followed these directions :
>> http://railswiki.pdxruby.org/HowToDefineYourOwnDat...
>>
>> It seems pretty straightforward but obviously it doesn't work for me...
>> article.date_edited.to_formatted_s(:my_format_1)
>>
>> produce a default formatted date, not the one I've defined in environment.rb :
>> ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
>>   :my_format_1 => '%d/%m/%Y - %H:%M',
>>   :my_format_2  => '%l:%M %p, %B %d, %Y'
>> )
>
> What does "it doesn't work for me" mean?

It means that my_format_1 has no effect at all when used with
to_formatted_s, Rails displays the default date format instead.

>
> I'm doing this and it works fine. Just make sure the format definitions
> in your environment.rb go outside the run block.

It is

>
> By the way, you can just use some_date.to_s(:format) - Date#to_s is
> aliased to Date#to_formatted_s
>

Thank for the hint Josh,

I'm working on a Rails Engine and I wonder how it could be related with
the problem ? I really don't understand...

Just to be sure, here is the content of the files again :

environment.rb :
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :my_format_1 => '%d/%m/%Y - %H:%M',
  :my_format_2  => '%l:%M %p, %B %d, %Y'
)

the view - list.rhtml
<%= article.date_edited.to_s(:my_format_1) %>
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-23 23:03
Christophe Gimenez wrote:
> Just to be sure, here is the content of the files again :
>
> environment.rb :
> ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
>   :my_format_1 => '%d/%m/%Y - %H:%M',
>   :my_format_2  => '%l:%M %p, %B %d, %Y'
> )
>
> the view - list.rhtml
> <%= article.date_edited.to_s(:my_format_1) %>

That looks to me like it should work. I suggest checking things out in
the console. Go in script/console and examine
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS to make
sure your format additions worked, then try printing
Date.today.to_s(:my_format_1).  Also, look in the database and make sure
there is a date in the field, try printing it with a different format,
etc.

--josh
http://blog.hasmanythrough.com
9c0eebc506c56c64dc616cdf123fa671?d=identicon&s=25 Chris Chris (aboulafia)
on 2006-03-23 23:58
I've tried with the console and everything is ok :
my_format_1 is listed and Date.today.to_s(:my_format_1) returns a date
formatted accordingly to the format...

The record in the db does contain a date value and trying different
formats (eg. :short) works fine in the view

I really don't understand...

But at least I'm learning about the console ! Great tool !
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-24 00:03
Christophe Gimenez wrote:
> I've tried with the console and everything is ok :
> my_format_1 is listed and Date.today.to_s(:my_format_1) returns a date
> formatted accordingly to the format...
>
> The record in the db does contain a date value and trying different
> formats (eg. :short) works fine in the view
>
> I really don't understand...
>
> But at least I'm learning about the console ! Great tool !

Last thing I can think of is to quit and restart your server. I think
you have to do that to get changes in the environment.rb to load.

--josh
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.