# Noob DateTime question

**URL:** <https://www.ruby-forum.com/t/noob-datetime-question/112217>\
**Category:** Rails\
**Created:** [August 16, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/noob-datetime-question/112217 "2007-08-16T13:31:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [August 16, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/noob-datetime-question/112217/1 "2007-08-16T13:31:56Z")

</div>

Hi,

I have a database column defined as datetime.  
When i display \<%= orders.orderdate %\> in the view i get:

Thu Aug 16 12:22:53 +0100 2007

Which is as expected. What I would like though is to just display the  
time portion of the column.

I think i need to convert the orders.orderdate to a time so i can do  
something like: time.strftime(’%H:%M’) but i’m not sure!

Its such a simple thing, but its taking me ages to work out!

Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [August 16, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/noob-datetime-question/112217/2 "2007-08-16T13:50:53Z")

</div>

Hi, you can define an instance method on the Order model class that  
returns  
the time portion of the datetime field. For example,  
def get\_time  
self.orderdate.strftime(’%H:%M’)  
end

Now, self is referencing the current Order instance.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [August 16, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/noob-datetime-question/112217/3 "2007-08-16T15:55:12Z")

</div>

That would work, and you can also use the hour, min, sec methods in the  
time class if you need them separately.  
[http://www.ruby-doc.org/core/classes/Time.html](http://www.ruby-doc.org/core/classes/Time.html)

Mike Rowlands wrote:

> I think i need to convert the orders.orderdate to a time so i can do  
> something like: time.strftime(‘%H:%M’) but i’m not sure!
> 
> Its such a simple thing, but its taking me ages to work out!
> 
> Thanks,
> 
> Mike

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid
