Aaron Vegh
Aaron Vegh

Reputation: 5217

Rails comparing dates

I have a data model that contains a DateTime attribute, date. I want to find out if that date is today. I'm having a hell of a time!

>> Deal.first.date 
=> Mon, 14 Dec 2009 23:59:59 EST -05:00

I've tried so many techniques that I would be embarrassed to put them here. So I would just ask, what do I need to match this date in an ActiveRecord find method?

Thanks, Aaron.

Upvotes: 3

Views: 3994

Answers (3)

klochner
klochner

Reputation: 8125

I'm guessing the time zone is driving you batty.

For a specific instance, you can do:

 model.date.today?

For an activerecord find, try this:

 Deal.all(:conditions=>['date > ?',Time.zone.now.beginning_of_day])

Here's the problem:

>> Time.zone.now.beginning_of_day == Date.today.beginning_of_day
=> false

Upvotes: 5

sarahhodne
sarahhodne

Reputation: 10114

I think this should work, not sure if it will though, as I haven't tested it.

Deal.first(:condition => { :date => Date.today })

Upvotes: -1

Mladen Jablanović
Mladen Jablanović

Reputation: 44110

I'd go with something like this:

Deal.find(:first, :conditions => ['date >= ? AND date < ?', Date.today, Date.today+1])

Upvotes: 0

Related Questions