I need to subtract two DateTime
objects in order to find out the difference in hours between them.
I try to do the following:
a = DateTime.new(2015, 6, 20, 16)
b = DateTime.new(2015, 6, 21, 16)
puts a - b
I get (-1/1)
, the object of class Rational
.
So, the question is, how do I find out what the difference betweent the two dates is? In hours or days, or whatever.
And what does this Rational
mean/represent when I subtract DateTimes just like that?
BTW:
When I try to subtract DateTime
's with the difference of 1 year, I get (366/1)
, so when I do (366/1).to_i
, I get the number of days. But when I tried subtracting two DateTime
's with the difference of 1 hour, it gave me -1
, the number of hours. So, how do I also find out the meaning of the returned value (hours, days, years, seconds)?
When you substract two datetimes, you'll get the difference in days, not hours.
You get a
Rational
type for the precision (some float numbers cannot be expressed exactly with computers)To get a number of hours, multiply the result by 24, for minutes multiply by 24*60 etc...
Here's a link to the official doc