How to get hours and minutes from 'datetime.timedelta' object?
John Machin
sjmachin at lexicon.net
Mon Aug 7 09:09:54 EDT 2006
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.
More information about the Python-list
mailing list