This is my code:
import datetime
today = datetime.date.today()
print today
This prints: 2008-11-22 which is exactly what I want BUT....I have a list I'm appending this to and then suddenly everything goes "wonky". Here is the code:
import datetime
mylist = []
today = datetime.date.today()
mylist.append(today)
print mylist
This prints the following:
[datetime.date(2008, 11, 22)]
How on earth can I get just a simple date like "2008-11-22"?
Use date.strftime. The formatting arguments are described in the documentation.
This one is what you wanted:
This one takes Locale into account. (do this)
You need to convert the date time object to a string.
The following code worked for me:
Let me know if you need any more help.
Edit:
After Cees suggestion, I have started using time as well:
Here is how to display the date as (year/month/day) :
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 stringdatetime.date.today()
, which you had previously set as the value of yourtoday
variable, rather than just appendingtoday()
.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:
and it prints
yyyy-mm-dd
.this will print 6-23-2018 if that's what you want :)