# Date time comparison

**URL:** <https://www.ruby-forum.com/t/date-time-comparison/106838>\
**Category:** Rails\
**Created:** [June 14, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/date-time-comparison/106838 "2007-06-14T15:07:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paolo\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@Paolo\_N](https://www.ruby-forum.com/u/Paolo_N)\
**Post date:** [June 14, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/date-time-comparison/106838/1 "2007-06-14T15:07:30Z")

</div>

Hi there

in RoR i can convert a Time into a Date object

t = Time.new  
=\> Thu Jun 14 14:00:46 BST 2007  
d = Date.new(2007,06,03)  
#\<Date: 4908509/2,0,2299161\>  
t.to\_date  
=\> #\<Date: 4908531/2,0,2299161\>  
d.to\_time  
=\> Sun Jun 03 00:00:00 BST 2007

But I can not compare Date and Time object

t \> d

ArgumentError: comparison of Time with Date failed

Now, am I shooting myself in the foot if I do something like

class Date  
alias oldcmp \<=\>  
def \<=\>(other)  
if other.respond\_to? :to\_date  
oldcmp(other.to\_date)  
else  
oldcmp(other)  
end  
end  
end

class Time  
alias oldcmp \<=\>  
def \<=\>(other)  
if other.respond\_to? :to\_time  
oldcmp(other.to\_time)  
else  
oldcmp(other)  
end  
end  
end

To allow comparison among Date and Time objects?

Thanks

Paolo
