Forum: Ruby on Rails Date Time format in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nike M. (Guest)
on 2009-04-21 17:26
Can anyone  give a suggestion so that i can able to format the date as
like this 3th - 5th June 2009
Rob B. (Guest)
on 2009-04-21 19:06
(Received via mailing list)
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@(irb):7>
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
Shaun K. (Guest)
on 2009-04-22 12:16
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@(irb):7>
> 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.
This topic is locked and can not be replied to.