Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date/Time comparison in ruby

I have these dates and times:

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

I am trying to detect all schedules that start 60 minutes or less before day_start. With the following code, I get as a response "NO" instead of "YES".

if schedule.day_start < (Time.now + 60.minutes)
  "YES"
else
  "NO"
end

Why is 2014-09-27 15:30:00 UTC bigger than 2014-09-27 16:11:14 +0200?

like image 573
user984621 Avatar asked Sep 27 '14 13:09

user984621


1 Answers

Work them dates as UTC, so you will avoid time zone problems

if schedule.day_start.utc < (Time.now + 60.minutes).utc
  ...
like image 180
Benj Avatar answered Oct 22 '22 22:10

Benj