how to get the time difference from 2 DateTime objects

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

  1. 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)

    Thanks in advance
     
    Schüle Daniel, Nov 21, 2006
    #1
    1. Advertising

  2. 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)
    >
    > Thanks in advance


    >> 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
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    710
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    536
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    392
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    338
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    762
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page