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

Use the ordinalize method from ‘active_support’.

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

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

require 'active_support/core_ext/integer/inflections'

Leave a Comment