How to convert a timedelta object to a string?

Bryan Olson fakeaddress at nowhere.org
Thu Sep 14 18:22:23 EDT 2006


Paul McGuire wrote:
> "Carl J. Van Arsdall" wrote:
>> Basically I used the datetime module and timedelta objects to calculate a 
>> difference between two times.  Now I'm trying to figure out how I make 
>> that time delta a string HH:MM:SS to show elapsed time.
[...]

> From the Python console:
> 
>>>> startTime = datetime.timedelta(seconds=45,minutes=22,hours=10)
>>>> stopTime = datetime.timedelta(seconds=25,minutes=2,hours=4)
>>>> delta = startTime-stopTime
>>>> time.strftime("%H:%M:%S",time.gmtime(delta.seconds))
> '06:20:20'

On the other hand:

 >>> startTime = datetime.timedelta(hours=28)
 >>> stopTime = datetime.timedelta(hours=4)
 >>> delta = startTime-stopTime
 >>> time.strftime("%H:%M:%S",time.gmtime(delta.seconds))
'00:00:00'


-- 
--Bryan



More information about the Python-list mailing list