Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if two timestamps are the same day in Ruby

Tags:

date

time

ruby

I'm a bit confused between Date, Datetime, and Time in Ruby. What's more, my application is sensitive to timezones, and I'm not sure how to convert between these three while being timezone-robust.

How can I check if two unix timestamps (seconds since epoch) represent the same day? (I don't actually mind if it uses local time or UTC; while I'd prefer local time, as long as it's consistent, I can design around that).

like image 430
chimeracoder Avatar asked Dec 18 '11 08:12

chimeracoder


1 Answers

Using the standard library, convert a Time object to a Date.

require 'date'  Time.at(x).to_date === Time.at(y).to_date 

Date has the === method that will be true if two date objects represent the same day.

like image 143
Anurag Avatar answered Sep 21 '22 06:09

Anurag