Extending Date/Time Formats in Rails 3

12 August 2011 - 22:33
Permalink

One of the many under-the-hood changes in Rails 3.x involves how date and time formats are extended. This is still done in an initializer, but the Rails classes have changed.

Here’s how it was done in Rails 2.x (for example):

  1. Rails 2.x
  2. /config/initializers/date_formats.rb
    ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
    :teg_compact => “%m/%d/%Y %H:%M”,
    :teg_long => “%d %B %Y – %H:%M”,
    :teg_date => “%m/%d/%Y”,
    :teg_label => “%d%B%Y-%H%M”
    )

And here’s how it’s done in Rails 3.x:

  1. Rails 3.x
  2. /config/initializers/date_formats.rb
    Time::DATE_FORMATS.merge!(
    :teg_compact => “%m/%d/%Y %H:%M”,
    :teg_long => “%d %B %Y – %H:%M”,
    :teg_date => “%m/%d/%Y”,
    :teg_label => “%d%B%Y-%H%M”
    )

Comments

No comments yet.

To comment, you must log in first.


Latest News

Musings, reports, and notes to ourselves about technical matters of importance.