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

Ant antroy at gmail.com
Mon Aug 7 06:54:15 EDT 2006


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.




More information about the Python-list mailing list