开发者

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 b开发者_StackOverflow中文版e 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.


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


I'd go with something like this:

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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜