How to print a date in a regular format?

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

    A quick disclaimer for my answer - I've only been learning Python for about 2 weeks, so I am by no means an expert; therefore, my explanation may not be the best and I may use incorrect terminology. Anyway, here it goes.

    I noticed in your code that when you declared your variable today = datetime.date.today() you chose to name your variable with the name of a built-in function.

    When your next line of code mylist.append(today) appended your list, it appended the entire string datetime.date.today(), which you had previously set as the value of your today variable, rather than just appending today().

    A simple solution, albeit maybe not one most coders would use when working with the datetime module, is to change the name of your variable.

    Here's what I tried:

    import datetime
    mylist = []
    present = datetime.date.today()
    mylist.append(present)
    print present
    

    and it prints yyyy-mm-dd.

提交回复
热议问题