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. Advertisements

  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. Advertisements

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:
    995
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. mp
    Replies:
    1
    Views:
    648
    John Machin
    Jul 28, 2006
  3. Will Rocisky
    Replies:
    1
    Views:
    431
    Steven D'Aprano
    Aug 5, 2008
  4. Martin
    Replies:
    0
    Views:
    610
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    1,029
    M.-A. Lemburg
    Jan 6, 2009
  6. NiklasRTZ

    datetime.datetime. or datetime. ?

    NiklasRTZ, Oct 8, 2009, in forum: Python
    Replies:
    10
    Views:
    717
    niklasr
    Oct 11, 2009
  7. Jaap van Wingerde
    Replies:
    0
    Views:
    98
    Jaap van Wingerde
    Mar 3, 2014
  8. Jaap van Wingerde
    Replies:
    5
    Views:
    208
    Roy Smith
    Mar 4, 2014
Loading...