Search code examples
rubystrftime

How do I format a date in ruby to include "rd" as in "3rd"


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?


Solution

  • 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"