开发者

Why is Rails date comparison not working?

开发者 https://www.devze.com 2022-12-30 13:20 出处:网络
What am I missing here, it\'s driving me crazy.. >> user.current_login_at.utc > 24.hours.ago.utc

What am I missing here, it's driving me crazy..

>> user.current_login_at.utc > 24.hours.ago.utc
=> false
&g开发者_StackOverflow中文版t;> 24.hours.ago.utc
=> Mon May 17 18:46:16 UTC 2010
>> user.current_login_at.utc
=> Mon May 17 15:47:44 UTC 2010

user.current_login_at was 27 hours ago, yet the greater than comparison says it was not greater than 24 hours ago. It leaves me scratching my head..


"Greater than" for a date means "after". 24.hours.ago gives a time, not a length of time. Conceptually, you are asking "did the user log in after the time that was 24 hours ago?", not "did the user log in more than 24 hours ago?". So you want:

user.current_login_at.utc < 24.hours.ago.utc

Which says "did the user log in before 24 hours ago?"


You are using "greater than", which in date comparisons, means "later than"

Is current login later than 24 hours ago?

user.current_login_at.utc > 24.hours.ago.utc

Is current login earlier than 24 hours ago?

user.current_login_at.utc < 24.hours.ago.utc
0

精彩评论

暂无评论...
验证码 换一张
取 消