Jonathan Tran Jonathan Tran - 1 year ago 79
Ruby Question

In Ruby on Rails, how do I format a date with the "th" suffix, as in, "Sun Oct 5th"?

I want to display dates in the format: short day of week, short month, day of month without leading zero but including "th", "st", "nd", or "rd" suffix.

For example, the day this question was asked would display "Thu Oct 2nd".

I'm using Ruby 1.8.7, and Time.strftime just doesn't seem to do this. I'd prefer a standard library if one exists.

Answer Source

Use the ordinalize method from 'active_support'.

>> time =
=> Fri Oct 03 01:24:48 +0100 2008
>> time.strftime("%a %b #{}")
=> "Fri Oct 3rd"

Note, if you are using IRB with Ruby 2.0, you must first run:

require 'active_support/core_ext/integer/inflections'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download