tags:

views:

232

answers:

2

Hi,

how do i subtract two different UTC dates in Ruby and then get the difference in minutes?

Thanks

+1  A: 

(time1 - time2) / 60

If the time objects are string, Time.parse(time) them first

Chubas
+1  A: 

If you subtract two Date or DateTime objects, the result is a Rational representing the number of days between them. What you need is:

a = Date.new(2009, 10, 13) - Date.new(2009, 10, 11)
(a * 24 * 60).to_i   # 2880 minutes

or

a = DateTime.new(2009, 10, 13, 12, 0, 0) - DateTime.new(2009, 10, 11, 0, 0, 0)
(a * 24 * 60).to_i   # 3600 minutes
Alex - Aotea Studios