比较栏目中的日期 [英] Comparing dates in rails

查看:147
本文介绍了比较栏目中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个标准的 Post.first.created_at datetime。我可以直接与日期时间进行比较,格式为 2009-06-03 16:57:45.608000 -04:00 通过执行以下操作:

  Post.first.created_at> Time.parse(2009-06-03 16:57:45.608000 -04:00)

编辑:这两个字段都是 datetimes 不是日期

解决方案

是的,您可以使用比较运算符比较日期,例如:

  irb(main):018:0> ;昨天= Date.new(2009,6,13)
=> #< Date:4909991 / 2,0,2299161>
irb(main):019:0> Date.today>昨天
=>真的

但是您是否尝试将日期与日期时间进行比较?



如果是这样,你需要将datetime转换为日期,然后进行比较。



我希望这有助于。 p>

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 by doing something like:

Post.first.created_at > Time.parse("2009-06-03 16:57:45.608000 -04:00")

Edit: Both fields are datetimes, not dates.

解决方案

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

irb(main):018:0> yesterday = Date.new(2009,6,13)
=> #<Date: 4909991/2,0,2299161>
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.

这篇关于比较栏目中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆