Set the default date format for your Rails application

(Apologies to who I nicked this from – I can’t remember which book or blog post I found this)

The default string format for Dates is the ‘Database’ format which looks like this:

Date.today.to_s => "2008-09-24" 

So, this means if you have dates in your views they’ll default to that unless you force the format.

It’s really easy to set the default format though! Bung something like this in your environment.rb:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
:default => '%d %B %Y' )  

Where :default is the format you want to set. Now your dates will automatically default to a nice format!

Date.today.to_s => "24 September 2008" 

You can do the same with the time format:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:default => '%d %B %Y'
)  


Subscribe via RSS

Back to all blog posts