Number of days between two Time instances

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

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

require 'date'
days_between = (Date.parse(laterTime.to_s) - Date.parse(earlyTime.to_s)).round

Edit ...or more simply...

require 'date'
(laterTime.to_date - earlyTime.to_date).round

[1] pry(main)> earlyTime = Time.at(123)
=> 1970-01-01 01:02:03 +0100
[2] pry(main)> laterTime = Time.now
=> 2014-04-15 11:13:40 +0200
[3] pry(main)> (laterTime.to_date - earlyTime.to_date).to_i
=> 16175

earlyTime = Time.at(123)
laterTime = Time.now
time_difference = laterTime - earlyTime
time_difference_in_days = time_difference / 1.day  # just divide by 1.day

Tags:

Time

Ruby

Date