Custom Formats for DateTime

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.

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.