How to print a date in a regular format?

后端 未结 22 2759
孤街浪徒
孤街浪徒 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:42

    Considering the fact you asked for something simple to do what you wanted, you could just:

    import datetime
    str(datetime.date.today())
    
    0 讨论(0)
  • 2020-11-21 23:43

    You can do:

    mylist.append(str(today))
    
    0 讨论(0)
  • 2020-11-21 23:44

    Or even

    from datetime import datetime, date
    
    "{:%d.%m.%Y}".format(datetime.now())
    

    Out: '25.12.2013

    or

    "{} - {:%d.%m.%Y}".format("Today", datetime.now())
    

    Out: 'Today - 25.12.2013'

    "{:%A}".format(date.today())
    

    Out: 'Wednesday'

    '{}__{:%Y.%m.%d__%H-%M}.log'.format(__name__, datetime.now())
    

    Out: '__main____2014.06.09__16-56.log'

    0 讨论(0)
  • 2020-11-21 23:45
    # convert date time to regular format.
    
    d_date = datetime.datetime.now()
    reg_format_date = d_date.strftime("%Y-%m-%d %I:%M:%S %p")
    print(reg_format_date)
    
    # some other date formats.
    reg_format_date = d_date.strftime("%d %B %Y %I:%M:%S %p")
    print(reg_format_date)
    reg_format_date = d_date.strftime("%Y-%m-%d %H:%M:%S")
    print(reg_format_date)
    

    OUTPUT

    2016-10-06 01:21:34 PM
    06 October 2016 01:21:34 PM
    2016-10-06 13:21:34
    
    0 讨论(0)
  • 2020-11-21 23:45

    For those wanting locale-based date and not including time, use:

    >>> some_date.strftime('%x')
    07/11/2019
    
    0 讨论(0)
  • 2020-11-21 23:46

    This is shorter:

    >>> import time
    >>> time.strftime("%Y-%m-%d %H:%M")
    '2013-11-19 09:38'
    
    0 讨论(0)
提交回复
热议问题