Date Time format in Ruby


#1

Can anyone give a suggestion so that i can able to format the date as
like this 3th - 5th June 2009


#2

On Apr 21, 2009, at 9:26 AM, Nike M. wrote:

Can anyone give a suggestion so that i can able to format the date as
like this 3th - 5th June 2009

irb> require ‘date’
=> []
irb> Date::MONTHNAMES
=> [nil, “January”, “February”, “March”, “April”, “May”, “June”,
“July”, “August”, “September”, “October”, “November”, “December”]
irb> myformat = lambda {|date| “%s %s %d”%[date.mday.ordinalize,
Date::MONTHNAMES[date.month], date.year] }
=> #Proc:0x00007ff7862efcb8@:7(irb)
irb> myformat[Date.today]
=> “21st April 2009”

That ought to be enough of a suggestion! 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

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#3

Rob B. wrote:

On Apr 21, 2009, at 9:26 AM, Nike M. wrote:

Can anyone give a suggestion so that i can able to format the date as
like this 3th - 5th June 2009

irb> require ‘date’
=> []
irb> Date::MONTHNAMES
=> [nil, “January”, “February”, “March”, “April”, “May”, “June”,
“July”, “August”, “September”, “October”, “November”, “December”]
irb> myformat = lambda {|date| “%s %s %d”%[date.mday.ordinalize,
Date::MONTHNAMES[date.month], date.year] }
=> #Proc:0x00007ff7862efcb8@:7(irb)
irb> myformat[Date.today]
=> “21st April 2009”

That ought to be enough of a suggestion! 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

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid

Or you could use Date.today.strftime("%d %m %Y"), though you wouldn’t
get the extension (‘st’). Use a simple case statement on the last digit
if you can’t or won’t use ActiveSupport.