How to print a date in a regular format?

后端 未结 22 2651
孤街浪徒
孤街浪徒 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.

    0 讨论(0)
  • 2020-11-21 23:52

    You may want to append it as a string?

    import datetime 
    mylist = [] 
    today = str(datetime.date.today())
    mylist.append(today) 
    print mylist
    
    0 讨论(0)
  • 2020-11-21 23:53

    Use date.strftime. The formatting arguments are described in the documentation.

    This one is what you wanted:

    some_date.strftime('%Y-%m-%d')
    

    This one takes Locale into account. (do this)

    some_date.strftime('%c')
    
    0 讨论(0)
  • 2020-11-21 23:53

    Here is how to display the date as (year/month/day) :

    from datetime import datetime
    now = datetime.now()
    
    print '%s/%s/%s' % (now.year, now.month, now.day)
    
    0 讨论(0)
提交回复
热议问题