SundayMonday SundayMonday - 6 months ago 12
Ruby Question

Number of days between two Time instances

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).

Answer

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

(t1 - t2).to_i / (24 * 60 * 60)
Comments