Formatting Dates
You want to convert a date into a string and format it in a certain way.
Apply format string to the date object.
For example:
>>> from datetime import datetime >>> d = datetime.now() >>> “{:%Y-%m-%d %H:%M:%S}”.format(d) ‘2013-05-02 16:00:45’ >>> |
Discussion
The Python formatting language includes some special symbols for formatting date. %Y, %m, and %d correspond to year, month, and day numbers, respectively.
See Also
See Recipe 7.1 for formatting of numbers.
Formatting in Python actually involves a whole formatting language. You will find full details of this at Python’s website.