Re: How to find out a date/time difference

Discussion in 'Python' started by Nico Grubert, May 24, 2006.

  1. Nico Grubert

    Nico Grubert Guest


    > I use datetime class in my program and now
    > I have two fields that have the datetime format like this
    > datetime.datetime(2006, 5, 24, 16, 1, 26)


    > How can I find out the date/time difference ( in days) of such two
    > fields?


    Hi Lad,

    you could do this:

    >>> a = datetime.datetime(2006, 5, 24, 16, 1, 26)
    >>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
    >>> a-b

    datetime.timedelta(4)
    # 4 days

    >>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
    >>> x = a-b
    >>> x

    datetime.timedelta(4, 14400)
    >>> str(x)

    '4 days, 4:00:00'


    Regards,
    Nico
     
    Nico Grubert, May 24, 2006
    #1
    1. Advertising

  2. Nico Grubert

    Guest

    And if you want the number of days:

    py> d = datetime.datetime(2006,5,24,16,34) -
    datetime.datetime(2006,5,23,12,1)
    py> d.days
    1
    py> d = datetime.datetime(2006,5,24,16,34) -
    datetime.datetime(2006,5,23,19,1)
    py> d.days
    0
     
    , May 24, 2006
    #2
    1. Advertising

  3. Nico Grubert skrev:

    > you could do this:
    >
    > >>> a = datetime.datetime(2006, 5, 24, 16, 1, 26)
    > >>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
    > >>> a-b

    > datetime.timedelta(4)
    > # 4 days


    Or

    #v+

    >>> print (a-b).days

    4
    >>>


    #v-

    Mvh,

    --
    Klaus Alexander Seistrup
    SubZeroNet, Copenhagen, Denmark
    http://magnetic-ink.dk/
     
    Klaus Alexander Seistrup, May 24, 2006
    #3
    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. Matt
    Replies:
    1
    Views:
    604
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,267
    Michael Borgwardt
    May 30, 2004
  3. Drew
    Replies:
    5
    Views:
    16,305
    Roedy Green
    Jan 25, 2006
  4. Matt
    Replies:
    2
    Views:
    518
    Pete Becker
    Nov 8, 2003
  5. Lad
    Replies:
    2
    Views:
    290
    Klaus Alexander Seistrup
    May 24, 2006
Loading...

Share This Page