SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Tuesday, April 21, 2009

Date time format with suffix in ruby on rails

For formatting the date time with suffixes like (st,th,rd,nd) rails provides ordinalize helper which will truns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

>> time = Time.new
=> Tue Apr 21 19:11:13 +0000 2009
>> time.strftime("%a %b #{time.ordinalize}")
=> "Tue Apr 21st"

Note that this is inside a Rails console so if you did this in plain Ruby, you'd have to require 'rubygems' and require 'activesupport' to get Fixnum#ordinalize


No comments :

Post a Comment