Forum: Ruby on Rails compare Date with TIme

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B7fc51c081c5c587c469d47c5949afef?d=identicon&s=25 scott (Guest)
on 2006-06-12 11:47
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
B7fc51c081c5c587c469d47c5949afef?d=identicon&s=25 scott (Guest)
on 2006-06-12 12:05
dont worry, found a Date class hidden away in the documentation

problem solved by doing @pDate = Date.new(pTime.year, pTime.month,
pTime.day)
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-06-12 12:14
(Received via mailing list)
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.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-06-12 14:31
(Received via mailing list)
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/

hth,
Bill
This topic is locked and can not be replied to.