Problem changing datetime format


#1

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


#2

This question may be too hard for those here :slight_smile:

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


#3

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:my_date_format
=> “%Y, %d %B, %l:%M%P”)

That should do it.


#4

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 B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#5

Well…

http://wiki.rubyonrails.com/rails/pages/HowToDefineYourOwnDateFormat