Custom Formats for DateTime

Gabe Berke-Williams

Date::DATE_FORMATS is quite helpful. It lets you do this:

Date.today.to_s(:custom)

with only this code in config/initializers/date_format.rb:

Date::DATE_FORMATS[:custom] = "%Y-%m-%d"

To do the same thing for DateTime instances, like created_at columns, use Time::DATE_FORMATS.

Time::DATE_FORMATS

To set the default format for either one, set DATE_FORMATS[:default]. This will cause <%= item.created_at %> to output "2011-08-16", with no extra work from you.