比较 Rails 中的日期

发布于 2024-07-24 12:13:14 字数 317 浏览 2 评论 0原文

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

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

编辑: 两个字段都是 < strong>日期时间,不是日期

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情别恋 2024-07-31 12:13:14

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

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

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

如果是这种情况,您需要将日期时间转换为日期,然后进行比较。

我希望这有帮助。

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.

睡美人的小仙女 2024-07-31 12:13:14

是的,您可以直接将 created_at ActiveRecord 日期/时间字段的值与常规 DateTime 对象(就像您可以通过解析字符串获得的对象)进行比较。

在一个项目中,我有一个 Value 对象,它有一个created_at日期时间对象:

imac:trunk luca$ script/console
Loading development environment (Rails 2.3.2)
>> Value.first.created_at
=> Fri, 12 Jun 2009 08:00:45 CEST 02:00
>> Time.parse("2009-06-03 16:57:45.608000 -04:00")
=> Wed Jun 03 22:57:45 0200 2009
>> Value.first.created_at > Time.parse("2009-06-03 16:57:45.608000 -04:00")
=> true

created_at字段定义为:

  create_table "values", :force => true do |t|
    [...]
    t.datetime "created_at"
  end

注意,如果您的字段是日期而不是日期时间,那么您需要将其转换为时间:

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

或解析日期:

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

否则你会得到一个:

ArgumentError: comparison of Date with Time failed

Yes you can compare directly the value of a created_at ActiveRecord date/time field with a regular DateTime object (like the one you can obtain parsing the string you have).

In a project i have a Value object that has a created_at datetime object:

imac:trunk luca$ script/console
Loading development environment (Rails 2.3.2)
>> Value.first.created_at
=> Fri, 12 Jun 2009 08:00:45 CEST 02:00
>> Time.parse("2009-06-03 16:57:45.608000 -04:00")
=> Wed Jun 03 22:57:45 0200 2009
>> Value.first.created_at > Time.parse("2009-06-03 16:57:45.608000 -04:00")
=> true

The created_at field is defined as:

  create_table "values", :force => true do |t|
    [...]
    t.datetime "created_at"
  end

N.B. if your field is a date and not a datetime, then you need to convert it to a time:

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

or parse a date:

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

otherwise you'll get a:

ArgumentError: comparison of Date with Time failed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文