How to get hours and minutes from 'datetime.timedelta' object?

Discussion in 'Python' started by Lad, Aug 7, 2006.

  1. Lad

    Lad Guest

    Hello,
    what is the best /easest way how to get number of hours and minutes
    from a timedelta object?
    Let's say we have
    aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
    bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
    so
    c=bb-aa
    will be
    datetime.timedelta(5, 6339, 437000)

    I can easily get days ( c.days)
    but
    I still can not figure out how easily to get hours and minutes
    Any idea?
    Thank you for help
    L.
    Lad, Aug 7, 2006
    #1
    1. Advertising

  2. Lad

    John Machin Guest

    Lad wrote:
    > Hello,
    > what is the best /easest way how to get number of hours and minutes
    > from a timedelta object?
    > Let's say we have
    > aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
    > bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
    > so
    > c=bb-aa
    > will be
    > datetime.timedelta(5, 6339, 437000)
    >
    > I can easily get days ( c.days)
    > but
    > I still can not figure out how easily to get hours and minutes
    > Any idea?



    WTF^H^H^H ... You got an answer to this question 5 days ago .....
    [thread copied below]
    8<-------------------------------
    Lad wrote:
    > Sybren Stuvel wrote:
    > > Lad enlightened us with:
    > > > How can I find days and minutes difference between two datetime
    > > > objects?
    > > > For example If I have
    > > > b=datetime.datetime(2006, 8, 2, 8, 57, 28, 687000)
    > > > a=datetime.datetime(2006, 8, 1, 18, 19, 45, 765000)


    > > diff = b - a


    > Ok, I tried


    > >>> diff=b-a
    > >>> diff

    > datetime.timedelta(0, 52662, 922000)
    > >>> diff.min

    > datetime.timedelta(-999999999)


    Reread the manual:

    1. "min" is minIMUM, not minUTES

    2. You need:

    >>> diff.days

    0
    >>> diff.seconds

    52662
    >>> diff.microseconds

    922000
    >>> minutes = (diff.seconds + diff.microseconds / 1000000.0) / 60.0
    >>> minutes

    877.71536666666668

    8<----------------------------
    John Machin, Aug 7, 2006
    #2
    1. Advertising

  3. Lad

    Ant Guest

    John Machin wrote:
    > Lad wrote:
    > > Hello,
    > > what is the best /easest way how to get number of hours and minutes
    > > from a timedelta object?

    ....
    > >>> diff.days

    > 0
    > >>> diff.seconds

    > 52662
    > >>> diff.microseconds

    > 922000
    > >>> minutes = (diff.seconds + diff.microseconds / 1000000.0) / 60.0
    > >>> minutes

    > 877.71536666666668


    I suspect what Lad wanted was something more like:

    >>> def secs_mins_hours(timed):

    .... total_secs = timed.seconds
    .... secs = total_secs % 60
    .... total_mins = total_secs / 60
    .... mins = total_mins % 60
    .... hours = total_mins / 60
    .... return (secs, mins, hours)
    >>> aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
    >>> bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
    >>> td = aa - bb
    >>> secs_mins_hours(td)

    (39, 45, 1)

    I'm surprised that the timedelta class hasn't got secs, mins and hours
    properties - they could be generated on the fly in a similar way.
    Ant, Aug 7, 2006
    #3
  4. Lad

    John Machin Guest

    Ant wrote:
    > John Machin wrote:
    > > Lad wrote:
    > > > Hello,
    > > > what is the best /easest way how to get number of hours and minutes
    > > > from a timedelta object?

    > ...
    > > >>> diff.days

    > > 0
    > > >>> diff.seconds

    > > 52662
    > > >>> diff.microseconds

    > > 922000
    > > >>> minutes = (diff.seconds + diff.microseconds / 1000000.0) / 60.0
    > > >>> minutes

    > > 877.71536666666668

    >
    > I suspect what Lad wanted was something more like:


    1. If that's what he wanted, it was a very peculiar way of asking. Do
    you suspect that he needs to be shown how to conver 877.7... minutes
    into hours, minutes and seconds???

    2. Please consider that the order of the result would be more
    conventionally presented as (hours, minutes, seconds) -- or do you
    suspect that the OP needs it presented bassackwards?

    >
    > >>> def secs_mins_hours(timed):

    > ... total_secs = timed.seconds
    > ... secs = total_secs % 60
    > ... total_mins = total_secs / 60
    > ... mins = total_mins % 60
    > ... hours = total_mins / 60
    > ... return (secs, mins, hours)
    > >>> aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
    > >>> bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
    > >>> td = aa - bb
    > >>> secs_mins_hours(td)

    > (39, 45, 1)
    >
    > I'm surprised that the timedelta class hasn't got secs, mins and hours
    > properties - they could be generated on the fly in a similar way.
    John Machin, Aug 7, 2006
    #4
  5. Lad

    Ant Guest

    John Machin wrote:
    ....
    > 1. If that's what he wanted, it was a very peculiar way of asking. Do
    > you suspect that he needs to be shown how to conver 877.7... minutes
    > into hours, minutes and seconds???


    Chill dude, It wasn't an attack :)

    The datetime class has hour, minute and second attributes that give the
    values of each as being in range(24) (hours) and range(60). i.e.
    integers. So an educated guess leads me to the conclusion that it is
    similar functionality that he wants from the timedelta class.

    > 2. Please consider that the order of the result would be more
    > conventionally presented as (hours, minutes, seconds) -- or do you


    Very good point. That would have been a tricky issue for the OP, and
    for that I apologise.

    > suspect that the OP needs it presented bassackwards?


    I think that you have that last word muddled. Not quite ass-backward,
    but close ;-)
    Ant, Aug 7, 2006
    #5
  6. Lad

    John Machin Guest

    Ant wrote:
    > John Machin wrote:
    > ...
    > > 1. If that's what he wanted, it was a very peculiar way of asking. Do
    > > you suspect that he needs to be shown how to conver 877.7... minutes
    > > into hours, minutes and seconds???

    >
    > Chill dude, It wasn't an attack :)


    I didn't think it was.

    >
    > The datetime class has hour, minute and second attributes that give the
    > values of each as being in range(24) (hours) and range(60). i.e.
    > integers. So an educated guess leads me to the conclusion that it is
    > similar functionality that he wants from the timedelta class.
    >
    > > 2. Please consider that the order of the result would be more
    > > conventionally presented as (hours, minutes, seconds) -- or do you

    >
    > Very good point. That would have been a tricky issue for the OP, and
    > for that I apologise.
    >
    > > suspect that the OP needs it presented bassackwards?

    >
    > I think that you have that last word muddled. Not quite ass-backward,
    > but close ;-)


    On the contrary. It means "ass-backward, and then some". Google
    "dictionary bassackwards" and read the first few hits.

    Cheers,
    John
    John Machin, Aug 7, 2006
    #6
    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. Rahmi Acar
    Replies:
    0
    Views:
    549
    Rahmi Acar
    Jul 17, 2003
  2. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    685
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  3. Tim Peters
    Replies:
    0
    Views:
    517
    Tim Peters
    Sep 9, 2003
  4. Replies:
    2
    Views:
    455
    Skip Montanaro
    Sep 1, 2007
  5. lemonhead71
    Replies:
    0
    Views:
    1,337
    lemonhead71
    Oct 3, 2012
Loading...

Share This Page