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

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Dec 11 15:52:12 CET 2013


[top posting fixed]

>
> On 11 December 2013 13:37, Mark Lawrence <breamoreboy at yahoo.co.uk
> <mailto:breamoreboy at yahoo.co.uk>> wrote:
>
>     On 11/12/2013 13:12, Jignesh Sutar wrote:
>
>              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
>
>
>     Old style
>
>     print('%02d:%02d:%04d' % (now.hour, now.minute, now.year))
>
>     New style
>
>     print('{}:{}:{}'.format(now.__hour, now.minute, now.year))
>
>     Sorry I can never remember the formatting types to go between {} so
>     look for them around here
>     http://docs.python.org/3/__library/string.html#__formatstrings
>     <http://docs.python.org/3/library/string.html#formatstrings>
>
>     --
>     My fellow Pythonistas, ask not what our language can do for you, ask
>     what you can do for our language.
>
>     Mark Lawrence
>
On 11/12/2013 13:55, Jignesh Sutar wrote:> Thanks Mark,
>
>     print('%02d:%02d:%04d' % (now.hour, now.minute, now.year))
>
>
> That works for;
> now = datetime.now()
>
> but not for;
> exe_time = endTime-startTime
>
>
> Thanks,
> Jignesh
>
>

You'll have to do the sums yourself based on the data for timedelta here 
http://docs.python.org/3/library/datetime.html#timedelta-objects, 
alternatively download a third party module such as 
http://labix.org/python-dateutil.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list