Comparing datetime and time results in a false result
I am using Mongolian and chronic gem. Chronic creates a Time object, and Mongoid Date creates a DateTime object. So in Mongoid, when I want to get today, I do something like this:
Lead.last.send('lead date') # => {DateTime}2015-03-30T00:00:00-04:00
In the Chronicle, when I go through today, I get this:
Chronic.parse('today') # => {Time}2015-03-30 23:00:00 -0400
And I compare two with == symbol, it produces false even though they are the same as date. I need the following query to give a result when the "date date" is for today:
Lead.where("lead date" => Chronic.parse('today'))
What options do I have?
+1
source to share
1 answer
Does this code exactly replicate your problem?
require 'chronic'
require 'date'
text = "2015-03-30T00:00:00-04:00"
datetime = DateTime.parse(text)
time = Chronic.parse(text)
datetime == time
#=> false
Use DateTime #to_time
method or Time method #to_datetime
:
datetime.to_time == time
#=> true
datetime == time.to_datetime
#=> true
0
source to share