Search code examples
ruby-on-railsruby-on-rails-3internationalizationrails-i18n

How do I format my created_at output correctly with I18n?


I am using this I18n file.

I am calling it in my view like this:

<td class="center"><%= l o.created_at %></td>

This is being outputted like this:

Mon, 22 May 2013 04:04:43 +0000

For starters, why is it displaying May 22, 2013 and not April 22?

When I do it in the console, I get this:

> o.created_at
 => Mon, 22 Apr 2013 04:04:43 UTC +00:00 

I don't want it to display the time, or rather would prefer to just say something like:

Monday, April 22, 2013 @ 4:04am

How do I do that?


Solution

  • You can add custom date/time formats to your translation file. To see what time-based substitutions are possible, consult a reference for strfime

    formats:
      default: ! '%Y-%m-%d'
      long: ! '%B %d, %Y'
      short: ! '%b %d'
      custom: ! '%A, %M %B, %Y @ %l:%M%P'
    

    In your view, you'd make use as follows:

    <%= l o.created_at, :format => :custom %>
    

    You may need to get rid of blank entries in your en.yml file to correct your translation errors.