C. Yee
C. Yee

Reputation: 197

Comparing 2 DateTime instances and it's giving me the wrong result

I have 2 DateTime instances

today = Sun, 25 Nov 2018 18:59:00 -0500
tomorrow = Mon, 26 Nov 2018 00:00:00 GMT +00:00

When I do

today >= tomorrow

it comes out false which is correct

When I do

today + 1.minute >= tomorrow

it comes out true which is incorrect. Why is it doing that when the Nov 25 should be less than Nov 26? Is it somehow computing just the time and not the date? If yes, how can I fix this?

Upvotes: 0

Views: 34

Answers (1)

Raj
Raj

Reputation: 22926

Note that today and tomorrow are in a different timezone. When you add 1.minute to today, both values become the same. Hence == returns true.

2.5.3 :001 > today = DateTime.parse('Sun, 25 Nov 2018 18:59:00 -0500')
 => Sun, 25 Nov 2018 18:59:00 -0500
2.5.3 :002 > tomorrow = DateTime.parse('Mon, 26 Nov 2018 00:00:00 GMT +00:00')
 => Mon, 26 Nov 2018 00:00:00 +0000
2.5.3 :003 > today >= tomorrow
 => false
2.5.3 :004 > today + 1.minute
 => Sun, 25 Nov 2018 19:00:00 -0500
2.5.3 :005 > today + 1.minute >= tomorrow
 => true
2.5.3 :006 > today + 1.minute == tomorrow
 => true

Upvotes: 1

Related Questions