Date Time format in Ruby

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

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:[email protected]: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
[email protected]

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:[email protected]: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
[email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs