# Changing date format of an object

**URL:** <https://www.ruby-forum.com/t/changing-date-format-of-an-object/142442>\
**Category:** Rails\
**Created:** [July 8, 2008, 12:33am UTC](https://www.ruby-forum.com/t/changing-date-format-of-an-object/142442 "2008-07-08T00:33:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [July 8, 2008, 12:33am UTC](https://www.ruby-forum.com/t/changing-date-format-of-an-object/142442/1 "2008-07-08T00:33:17Z")

</div>

I have an object that holds the date in the form of “Tue Jul 08  
14:18:00-0700 2008” and I want to only display the time in the format  
of 6:00-8:00 or just 6:00. The controller looks like this:

def show  
@events = Event.find(:all, :conditions =\> [‘date between ?  
and ?’, @date.strftime("%Y-%m") + ‘-01’,  
@date.next\_month.strftime("%Y-%m") + ‘-01’])  
end

I am thinking that I should write a function in the model that changes  
the format of this current object to what I want. Something like

def to\_s  
#some code to change the format  
end

and then append this function on the end of the event call like so:

def show  
@events = Event.find(:all, :conditions =\> [‘date between ?  
and ?’, @date.strftime("%Y-%m") + ‘-01’,  
@date.next\_month.strftime("%Y-%m") + ‘-01’]).to\_s  
end

but I have no idea if this is the right approach or what code to use  
in the to\_s function. Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [July 8, 2008, 2:59am UTC](https://www.ruby-forum.com/t/changing-date-format-of-an-object/142442/2 "2008-07-08T02:59:28Z")

</div>

Hi Dave.

Have you seen this Railscasts? [#31 Formatting Time - RailsCasts](http://railscasts.com/episodes/31)

It might get you started.

Best.  
Mike
