[Tutor] formatting datetime.timedelta to "HH:MM:SS"
eryksun
eryksun at gmail.com
Thu Dec 12 12:37:33 CET 2013
On Wed, Dec 11, 2013 at 8:37 AM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
>
> 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
For datetime's date, time, and datetime types, the __format__ method
passes the spec to strftime:
>>> t = datetime.datetime(2038,1,19,3,14,7)
>>> '{:%H:%M:%S}'.format(t)
'03:14:07'
If the format spec is empty, it uses __str__:
>>> format(t)
'2038-01-19 03:14:07'
But this is unrelated to timedelta, which lacks a custom __format__.
More information about the Tutor
mailing list