String formatting with datetime

suggest change

Any class can configure its own string formatting syntax through the __format__ method. A type in the standard Python library that makes handy use of this is the datetime type, where one can use strftime-like formatting codes directly within str.format:

>>> from datetime import datetime
>>> dt = datetime(2016, 7, 21)
>>> 'North America: {dt:%m/%d/%Y}.  ISO: {dt:%Y-%m-%d}.'.format(dt=dt)
'North America: 07/21/2016.  ISO: 2016-07-21.'

datetime.now() would use the current date instead of this fixed one. A full list of datetime formatters can be found in the official documentation.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents