# Formatting timestamp objects

**URL:** <https://www.ruby-forum.com/t/formatting-timestamp-objects/51911>\
**Category:** Rails\
**Created:** [January 9, 2006, 4:10am UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911 "2006-01-09T04:10:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 9, 2006, 4:10am UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911/1 "2006-01-09T04:10:53Z")

</div>

I want to print my timestamp objects in a specific format. I want to  
print  
a date like this:

Sunday, January 8 2006

I don’t want it to print January 08.

For the time, I want it to look like this:

9:08 pm

Not 09:08 and lower case PM. I created these methods:

def format\_date(date)  
date.strftime("%A, %B #{date.day} #{date.year}")  
end

def format\_time(date)  
if date.hour \> 12  
hour = date.hour - 12  
elsif date.hour == 0  
hour = 12  
else  
hour = date.hour  
end  
time = "#{hour}:#{date.min} "  
if date.hour \> 12  
time += “pm”  
else  
time += “am”  
end  
end

There has to be a better way to do this. Any ideas?

BTW - How do I know what the datatype of the properties that are created  
by  
AcitveRecord::Base are?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 9, 2006, 5:40am UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911/2 "2006-01-09T05:40:47Z")

</div>

Paul B. wrote:

> I want to print my timestamp objects in a specific format. I want to  
> print  
> a date like this:
> 
> Sunday, January 8 2006
> 
> I don’t want it to print January 08.
> 
> For the time, I want it to look like this:
> 
> 9:08 pm
> 
> Not 09:08 and lower case PM. I created these methods:

Try this for the time format

def format\_time(date)  
date.strftime("%I:%M %p") =~ /(0\*)([\d:]+) (\w+)/  
“#{$2} #{$3.downcase}”  
end

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 9, 2006, 6:26am UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911/3 "2006-01-09T06:26:25Z")

</div>

Try this; it makes your view code very clean and dry.  
[http://rails.techno-weenie.net/tip/2005/11/20/defining\_custom\_date\_time\_formats](http://rails.techno-weenie.net/tip/2005/11/20/defining_custom_date_time_formats)

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 9, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911/4 "2006-01-09T21:54:24Z")

</div>

Use a regexp to find 08 and replace it with 8? There’s not a cleaner  
way to  
do that?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 9, 2006, 6:28am UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911/5 "2006-01-09T06:28:07Z")

</div>

Paul B. wrote:

> I want to print my timestamp objects in a specific format. I want to  
> print  
> a date like this:
> 
> Sunday, January 8 2006
> 
> I don’t want it to print January 08.
> 
> For the time, I want it to look like this:
> 
> 9:08 pm
> 
> Not 09:08 and lower case PM. I created these methods:
> 
> def format\_date(date)  
> date.strftime("%A, %B #{date.day} #{date.year}")  
> end
> 
> There has to be a better way to do this. Any ideas?

Check out ActiveSupport Time and Date conversion modules. They extend  
to\_s to take a symbol specifying the format to use, eg  
“Time.now.to\_s(:db)” converts the time to the canonical database  
representation of a datetime. Then you can put something like the  
following in your environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!(  
:date\_time12 =\> “%m/%d/%Y %I:%M%p”,  
:date\_time24 =\> “%m/%d/%Y %H:%M”  
)

The use a regexp and .downcase to finish the job.

> BTW - How do I know what the datatype of the properties that are created  
> by  
> AcitveRecord::Base are?

AR::columns returns an array of columns.  
Try printing  
MyModel.columns.collect {|c| [c.name, c.type]}

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 9, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/formatting-timestamp-objects/51911/6 "2006-01-09T22:12:40Z")

</div>

Also, when I print Time.zone, I get “Eastern Standard Time”. How can I  
get  
“EST” instead?
