Comparing dates in rails

前端 未结 2 755
终归单人心
终归单人心 2020-12-16 09:53

Suppose I have a standard Post.first.created_at datetime. Can I compare that directly with a datetime in the format 2009-06-03 16:57:45.608000 -04:00

2条回答
  •  春和景丽
    2020-12-16 10:24

    Yes, you can use comparison operators to compare dates e.g.:

    irb(main):018:0> yesterday = Date.new(2009,6,13)
    => #
    irb(main):019:0> Date.today > yesterday
    => true
    

    But are you trying to compare a date to a datetime?

    If that's the case, you'll want to convert the datetime to a date then do the comparison.

    I hope this helps.

提交回复
热议问题