我有这些日期和时间:
schedule.day_start # => 2014-09-27 15:30:00 UTC
date_now = Time.now # => 2014-09-27 15:11:14 +0200
date_now + 60.minutes # => 2014-09-27 16:11:14 +0200
我正在尝试检测在 day_start
之前 60 分钟或更短时间开始的所有计划。使用以下代码,我得到的响应是 "NO"
而不是 "YES"
。
if schedule.day_start < (Time.now + 60.minutes)
"YES"
else
"NO"
end
为什么 2014-09-27 15:30:00 UTC
大于 2014-09-27 16:11:14 +0200
?
最佳答案
将它们的日期设置为 UTC,这样您就可以避免时区问题
if schedule.day_start.utc < (Time.now + 60.minutes).utc
...
关于ruby-on-rails - ruby 中的日期/时间比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075078/