Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of days between two Time instances

Tags:

date

time

ruby

How can I determine the number of days between two Time instances in Ruby?

> earlyTime = Time.at(123)
> laterTime = Time.now
> time_difference = laterTime - earlyTime

I'd like to determine the number of days in time_difference (I'm not worried about fractions of days. Rounding up or down is fine).

like image 327
SundayMonday Avatar asked Sep 06 '25 12:09

SundayMonday


1 Answers

Difference of two times is in seconds. Divide it by number of seconds in 24 hours.

(t1 - t2).to_i / (24 * 60 * 60)
like image 166
Sergio Tulentsev Avatar answered Sep 10 '25 03:09

Sergio Tulentsev