# Compare Date with TIme

**URL:** <https://www.ruby-forum.com/t/compare-date-with-time/63113>\
**Category:** Rails\
**Created:** [June 12, 2006, 11:47am UTC](https://www.ruby-forum.com/t/compare-date-with-time/63113 "2006-06-12T11:47:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [June 12, 2006, 11:47am UTC](https://www.ruby-forum.com/t/compare-date-with-time/63113/1 "2006-06-12T11:47:14Z")

</div>

Hi

When returning data from a database column set as a date field I get  
‘2006-06-06’

I am then have these two lines of code in my controller, taht gain the  
date I require.

pNow = Time.now  
@pDate = Time.local(pNow.year, pNow.month, 1)

At the monment one is set to a date data type while one is set to a Time  
data type.

how can i change these to be the same data type so i can run a \> if  
statment on both of them to compare whats greater than the other?

thanks  
scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [June 12, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/compare-date-with-time/63113/2 "2006-06-12T12:05:35Z")

</div>

dont worry, found a Date class hidden away in the documentation

problem solved by doing @pDate = Date.new(pTime.year, pTime.month,  
pTime.day)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [June 12, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/compare-date-with-time/63113/3 "2006-06-12T12:14:27Z")

</div>

Both Time and Date have methods to convert them into instances of the  
other, to\_date and to\_time respectively.

Time.now.to\_date  
Date.today.to\_time

-Jonathan.

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [June 12, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/compare-date-with-time/63113/4 "2006-06-12T14:31:57Z")

</div>

scott wrote:

> At the monment one is set to a date data type  
> while one is set to a Time data type.
> 
> how can i change these to be the same data  
> type so i can run a \> if statment on both of them  
> to compare whats greater than the other?

I think you’ll find what you need in the  
ActiveSupport::CoreExtensions::(Time | Date)::Conversions modules at  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/)

hth,  
Bill
