Andrew
Andrew

Reputation: 239197

How to customize Rails localized date format?

I am overriding a view from a Rails engine that has this code:

<%= l(item.publish_date, :format => :long) %>

The format of this becomes:

July 17, 2012 23:27

I would like to change this so that the date looks more like:

July 17th, 2012  # I don't even really want the time

However, I am completely new to how Rails localization works so I don't know how to find the documentation on how to customize this date format. What do I need to do?

Upvotes: 2

Views: 2738

Answers (1)

Dty
Dty

Reputation: 12273

See the guide here for details but you can specify a format in your yml

# config/locales/pirate.yml
pirate:
  time:
    formats:
      short: "arrrround %H'ish"

A good website that helps with that is goodstrftime. It will show you a preview of what your specified format will look like.

For example:

%B %d, %Y

July 18, 2012

Use active supports ordinalize method to get the 'st', 'rd', 'th' suffixes.

Upvotes: 4

Related Questions