Monday, February 26, 2007

Custom Date and Datetime formatting app wide

You can utilize the environment.rb file to overwrite defined settings for the date and time display in rails.

1. ActiveSupport::CoreExtensions::Time::Conversions::
DATE_FORMATS.update(:my_format => '%d %B %Y %H:%M')
2. ActiveSupport::CoreExtensions::Date::Conversions::
DATE_FORMATS.update(:my_format => '%d %B %Y')

In addition, you can also strip the time from presentation

1. ActiveSupport::CoreExtensions::Time::Conversions::
DATE_FORMATS.update(:my_format => '%d %B %Y')

You can call the symbol (:my_format) whatever you like for multiple formatting calls. And finally, to call it in the app:

1. modelname.created_at.to_s :my_format

e.g., in my show.rhtml for my project model:

1. @project.created_on.to_s :my_format

http://www.railsweenie.com/forums/2/topics/777

No comments :