Comparing a DateTime with a Time in Ruby
23 Jun 2010
For anyone that comes across this problem:
>> a = Time.now
=> Wed Jun 23 13:50:35 +0100 2010
>> b = DateTime.parse(a.to_s)
=> Wed, 23 Jun 2010 13:50:35 +0100
>> a
=> Wed Jun 23 13:50:35 +0100 2010
>> b
=> Wed, 23 Jun 2010 13:50:35 +0100
>> a == b
=> false
>> a.to_datetime == b
=> false
The solution is to use ===
>> a.to_datetime === b
=> true
From the docs:
Compares dates by Julian Day Number. When comparing two DateTime instances, or a DateTime with a Date, the instances will be regarded as equivalent if they fall on the same date in local time.