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

Jignesh Sutar jsutar at gmail.com
Wed Dec 11 14:12:18 CET 2013


>
> print str(exe_time).split('.')[0]


Sorry, I guess my question was why I can't use something similar to below
on exe_time (of type datetime.timedelta)? Rather than doing string
manipulation on decimals or colons to extract the same.

now = datetime.now()
print now.hour
print now.minute
print now.year


On 11 December 2013 12:43, David Robinow <drobinow at gmail.com> wrote:

> 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]
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131211/143a5ed5/attachment.html>


More information about the Tutor mailing list