Take the 2-minute tour ×

I want to format a date object so that I can display strings such as "3rd July" or "1st October". I can't find an option in Date.strftime to generate the "rd" and "st". Any one know how to do this?

share|improve this question
add comment

5 Answers

up vote 15 down vote accepted

I'm going to echo everyone else, but I'll just encourage you to download the activesupport gem, so you can just use it as a library. You don't need all of Rails to use ordinalize.

% gem install activesupport
% irb 
irb> require 'rubygems'
#=>  true
irb> require 'activesupport'
#=>  true
irb> 3.ordinalize
#=>  "3rd"
share|improve this answer
Good point. You can also get this functionality from the facets (facets.rubyforge.org) library - require 'facets' or, for just this method, require 'facets/integer/ordinal' –  Greg Campbell Jul 5 '09 at 17:42
How do we extract just the ordinalization? I want to do 3<span>th</span> –  Volte Mar 14 '13 at 15:47
Volte: 3.ordinalize.sub(/\w+/, '<span>\0</span>') –  rampion Mar 14 '13 at 15:58
add comment

Unless you're using Rails, add this ordinalize method (code shamelessly lifted from the Rails source) to the Fixnum class

class Fixnum
  def ordinalize
    if (11..13).include?(self % 100)
      case self % 10
        when 1; "#{self}st"
        when 2; "#{self}nd"
        when 3; "#{self}rd"
        else    "#{self}th"

Then format your date like this:

> now = Time.now
> puts now.strftime("#{now.day.ordinalize} of %B, %Y")
=> 4th of July, 2009
share|improve this answer
add comment
created_at.strftime("#{created_at.day.ordinalize} of %m, %y")

Will produce "4th of July, 2009"

share|improve this answer
I had to add a # in front of the { but this worked a treat Thanks! –  xenon May 5 '10 at 21:50
add comment

I don't think Ruby has it, but if you have Rails, try this:-

puts 3.ordinalize #=> "3rd"
share|improve this answer
It's also available in Facets. –  Pesto Jul 6 '09 at 2:08
add comment

Seems I'm revisiting this topic for a third time, so I've updated my gist with some extra comments / usage.


Cheers, Ian.

share|improve this answer
add comment

Your Answer


