Ordinal Dates in Rails

Though rather simple, here’s the code to display dates such as “August 17th, 2008”, or “September 1st, 2008”. These are helper methods located in my RAILS_ROOT/app/helpers/application_helper.rb. They are used around the site to display the dates.

# September 1st, 2007
def short_date(date)
  date.strftime("%B #{date.day.ordinalize}, %Y")
end

# September 1st
def shorter_date(date)
  date.strftime("%B #{date.day.ordinalize}")
end

The “%B” and “%Y” are symbols representing a part of a date. They are universal for the most part, so take a look at PHP’s date() manual for reference.

0 Comments

Add Your Comment


(Never displayed.)

(Optional)

(URLs are auto-converted. HTML will be stripped.)

Navigation

Projects

Currently cooperating with Richard Neary on something that concerns audio.

A blog is typically published on a set date, and tagged with a few keywords. This is all that can be said.