# how to get the time difference from 2 DateTime objects

Discussion in 'Ruby' started by Schüle Daniel, Nov 21, 2006.

1. ### Schüle DanielGuest

Hi,

how can I get the time difference in say seconds
between 2 DateTime objects? their difference returns
a rational number which semantic is unclear to me.
(I would expect to get some kind of TimeDelta object
with seconds as its method)

irb(main):107:0* i=DateTime.now;sleep 10;j=DateTime.now;
irb(main):108:0* j-i
=> Rational(10000217, 86400000000)

Schüle Daniel, Nov 21, 2006

2. ### Robert KlemmeGuest

On 21.11.2006 05:14, Schüle Daniel wrote:
> Hi,
>
> how can I get the time difference in say seconds
> between 2 DateTime objects? their difference returns
> a rational number which semantic is unclear to me.
> (I would expect to get some kind of TimeDelta object
> with seconds as its method)
>
> irb(main):107:0* i=DateTime.now;sleep 10;j=DateTime.now;
> irb(main):108:0* j-i
> => Rational(10000217, 86400000000)
>

>> d1 = DateTime.now; sleep 10; d2 = DateTime.now

=> #<DateTime: 70676958680866127/28800000000,1/12,2299161>
>> diff = d2 - d1

=> Rational(1, 8640)
>> diff.to_f

=> 0.000115740740740741
>> diff.to_f * 24 * 60 * 60

=> 10.0

robert

Robert Klemme, Nov 21, 2006