# How to get a time object from a date object?

**URL:** <https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289>\
**Category:** Ruby\
**Created:** [January 6, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289 "2007-01-06T19:32:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [January 6, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289/1 "2007-01-06T19:32:05Z")

</div>

lo there all, i have a date object, and i need to make a time object  
from  
it.  
i tried the to\_i, ( what i really need are the seconds ) but it failed.  
I am  
sure that there is an easy way to do this, i just can’t seem to find it.

thanks  
shawn

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [January 6, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289/2 "2007-01-06T19:45:01Z")

</div>

shawn bright wrote:

> lo there all, i have a date object, and i need to make a time object from  
> it.  
> i tried the to\_i, ( what i really need are the seconds ) but it failed.  
> I am  
> sure that there is an easy way to do this, i just can’t seem to find it.

Time.local(date.year, date.month, date.day)

(Courtesy of  
[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html),  
actually.)

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [January 6, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289/3 "2007-01-06T20:22:06Z")

</div>

cool thanks. all working now

shawn

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [January 6, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289/4 "2007-01-06T20:36:09Z")

</div>

Devin M. wrote:

> [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html),  
> actually.)

class DateTime  
def to\_time  
Time.local( year, month, day, hour, min, sec )  
end  
end

puts (DateTime.now - 14).to\_time

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [January 6, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-get-a-time-object-from-a-date-object/81289/5 "2007-01-06T20:43:10Z")

</div>

Devin M. wrote:

> [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html),  
> actually.)

class DateTime  
def to\_time  
Time.local( year, month, day, hour, min, sec )  
end  
end

puts (DateTime.now - 14).to\_time
