Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract dates in Ruby and get the difference in minutes

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

Thanks

like image 322
Mark Avatar asked May 17 '10 20:05

Mark


People also ask

How do you subtract dates in Ruby?

day age = date_now. year - dob. year else age = (date_now. year - dob.


1 Answers

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 
like image 90
Alex Korban Avatar answered Sep 25 '22 19:09

Alex Korban