How to convert a timedelta object to a string?

Paul McGuire ptmcg at austin.rr._bogus_.com
Thu Sep 14 17:34:33 EDT 2006


"Carl J. Van Arsdall" <cvanarsdall at mvista.com> wrote in message 
news:mailman.104.1158267229.10491.python-list at python.org...
> 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.  I've spent tons 
> of time looking at the module's documentation but I'm not seeing how those 
> methods will help me.  Can anyone help me with this?
> Here's what I'm trying to do, assum that resultsDict is a dictionary of 
> objects that have various pieces of information including a start time and 
> a stop time that are strings which I extracted from a file in an earlier 
> part of the program.  I use regEx to split up the hours, minutes, and 
> seconds and create timedelta objects, which I then subtract to get the 
> different.  What I need is to get the value in duration as HH:MM:SS as a 
> string:
>
>

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

-- Paul 





More information about the Python-list mailing list