Thursday, August 20, 2009

Getting Rid of the Zero in Front of Days in Ruby Dates

Here is a way to get rid of the preceding zero/zero prefix on day in a Ruby date:
>> time = Time.local(2009,"nov",9,23,59,59)
=> Mon Nov 09 23:59:59 -0500 2009
>> time.strftime("%a %b #{time.day}")
=> "Mon Nov 9"

Bartosz Blimke in this stackoverflow answer also shares the solution for how to ordinalize the day in a Ruby date:

>> time = Time.new
=> Fri Oct 03 01:24:48 +0100 2008
>> time.strftime("%a %b #{time.day.ordinalize}")
=> "Fri Oct 3rd"

No comments: