How to print a date in a regular format?

后端 未结 22 2755
孤街浪徒
孤街浪徒 2020-11-21 23:00

This is my code:

import datetime
today = datetime.date.today()
print(today)

This prints: 2008-11-22 which is exactly what I wa

22条回答
  •  北海茫月
    2020-11-21 23:52

    With type-specific datetime string formatting (see nk9's answer using str.format().) in a Formatted string literal (since Python 3.6, 2016-12-23):

    >>> import datetime
    >>> f"{datetime.datetime.now():%Y-%m-%d}"
    '2017-06-15'
    

    The date/time format directives are not documented as part of the Format String Syntax but rather in date, datetime, and time's strftime() documentation. The are based on the 1989 C Standard, but include some ISO 8601 directives since Python 3.6.

提交回复
热议问题