Forum: Ruby on Rails Problem changing datetime format

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.
Bfb4faac1ebe6bcd236d65cf98428632?d=identicon&s=25 reed (Guest)
on 2007-02-09 06:54
(Received via mailing list)
I am trying to format some datetimes like this...2007, February the
9th, 6:30pm

I have the following which gets me part way there.

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:my_date_format
=> "%Y, %d %B, %I:%M%p")

There are 2 problems
1. There is no "th" formatter so that I get 1st, 2nd, 3rd, 4th, 5th,
etc.
2. PM is in uppercase and I would prefer it in lower.

TIA
Bfb4faac1ebe6bcd236d65cf98428632?d=identicon&s=25 reed (Guest)
on 2007-02-10 09:20
(Received via mailing list)
This question may be too hard for those here :-)

I understand that 1.ordinalize is '1st' (Edge) and 'PM'.downcase is
'pm' but I dont see how it can be applied to Conversions for my custom
formatter.

Also the hours portion (%I) of the time format make 06 instead of 6,
really, normal people just don't write 06:30
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2007-02-10 13:08
(Received via mailing list)
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:my_date_format
=> "%Y, %d %B, %l:%M%P")

That should do it.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-02-11 00:42
(Received via mailing list)
On Feb 10, 2007, at 3:19 AM, reed wrote:
>> I am trying to format some datetimes like this...2007, February the
>> etc.
>> 2. PM is in uppercase and I would prefer it in lower.
>>
>> TIA

Since the DATE_FORMATS are simple strftime() formatting specs, you
can't just create that format.  However, you can roll your own (even
making it part of Time if you want) like this:

require 'rubygems'
gem 'activesupport', ">=1.4"
require 'active_support'

t = Time.now

class Time
   def my_time
     hr = hour % 12
     hr = 12 if hr.zero?
     "%d, %s the %s, %d:%02d%s" % [ year, strftime("%B"),
day.ordinalize,
                                    hr, min, strftime("%p").downcase ]
   end
end

puts t
puts t.my_time

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2007-02-11 02:24
(Received via mailing list)
This topic is locked and can not be replied to.