Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 618
Aaron Vegh Avatar asked Dec 11 '25 12:12

Aaron Vegh


1 Answers

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
like image 81
klochner Avatar answered Dec 13 '25 02:12

klochner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!