[Tutor] formatting datetime.timedelta to "HH:MM:SS"

David Robinow drobinow at gmail.com
Wed Dec 11 13:43:18 CET 2013


On Wed, Dec 11, 2013 at 5:55 AM, Jignesh Sutar <jsutar at gmail.com> wrote:
> Hi,
>
> I've googled around extensively to try figure this out assuming it should be
> straight forward (and it probably is) but I'm clearly missing something.
>
> I'm trying to get the total run time of the program but have the final time
> being displayed in a particular format. I.e. without the seconds in
> milliseconds decimal points and just to customize it a bit more.
>
> import time
> from datetime import datetime
> startTime = datetime.now()
> time.sleep(5.1564651443644)
> endTime = datetime.now()
> exe_time = endTime-startTime
>
> print type(startTime)
> print type(endTime)
> print type(exe_time)
>
> print "startTime: ", startTime
> print "endTime:", endTime
> print "exe_time: ", exe_time #how to format this to "D Days, HH: MM: SS" ?
> #exe_time:  0:00:05.156000
> #desired 0 Days, 0h: 00:m: 05s
>
print str(exe_time).split('.')[0]


More information about the Tutor mailing list