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

John Machin sjmachin at lexicon.net
Mon Aug 7 05:16:09 EDT 2006


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




More information about the Python-list mailing list