# Problem changing datetime format

**URL:** <https://www.ruby-forum.com/t/problem-changing-datetime-format/102681>\
**Category:** Rails\
**Created:** [February 9, 2007, 6:54am UTC](https://www.ruby-forum.com/t/problem-changing-datetime-format/102681 "2007-02-09T06:54:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 9, 2007, 6:54am UTC](https://www.ruby-forum.com/t/problem-changing-datetime-format/102681/1 "2007-02-09T06:54:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 10, 2007, 9:20am UTC](https://www.ruby-forum.com/t/problem-changing-datetime-format/102681/2 "2007-02-10T09:20:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 10, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/problem-changing-datetime-format/102681/3 "2007-02-10T13:08:03Z")

</div>

ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.update(:my\_date\_format  
=\> “%Y, %d %B, %l:%M%P”)

That should do it.

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 11, 2007, 12:42am UTC](https://www.ruby-forum.com/t/problem-changing-datetime-format/102681/4 "2007-02-11T00:42:11Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 11, 2007, 2:24am UTC](https://www.ruby-forum.com/t/problem-changing-datetime-format/102681/5 "2007-02-11T02:24:17Z")

</div>

Well…

[http://wiki.rubyonrails.com/rails/pages/HowToDefineYourOwnDateFormat](http://wiki.rubyonrails.com/rails/pages/HowToDefineYourOwnDateFormat)
