Custom date format


#1

Hi, I’ve followed these directions :
http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html

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


#2

Christophe G. wrote:

Hi, I’ve followed these directions :
http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html

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


#3

Josh S. wrote:

Christophe G. wrote:

Hi, I’ve followed these directions :
http://railswiki.pdxruby.org/HowToDefineYourOwnDateFormat.html

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) %>


#4

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 !


#5

Christophe G. 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


#6

Christophe G. 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