Ordinal Dates in Rails
Monday, September 1st, 2008 · 0 comments
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.
