This is my code:
import datetime
today = datetime.date.today()
print(today)
This prints: 2008-11-22
which is exactly what I wa
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
.