Find Records with Datetime that match today's date - Ruby on Rails

后端 未结 3 664
甜味超标
甜味超标 2021-01-31 16:23

I have a table of deals and need to find records where the date matches today\'s date.

From the rails console, the date field I need to match looks like this. I\'ve assi

3条回答
  •  忘掉有多难
    2021-01-31 16:42

    For those still on Rails 4:

    To augment not_a_patch's answer above, I would use:

    Deal.where(start: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
    

    because Time.zone will use UTC (which is how your date time field is stored) while DateTime.now will not.

    > DateTime.now
    => Fri, 08 Sep 2017 11:28:21 -0400
    > Time.zone.now
    => Fri, 08 Sep 2017 15:29:53 UTC +00:00
    

提交回复
热议问题