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"?
Since the
print today
returns what you want this means that the today object's__str__
function returns the string you are looking for.So you can do
mylist.append(today.__str__())
as well.In this way you can get Date formatted like this example: 22-Jun-2017
This is shorter:
I hate the idea of importing too many modules for convenience. I would rather work with available module which in this case is
datetime
rather than calling a new moduletime
.The WHY: dates are objects
In Python, dates are objects. Therefore, when you manipulate them, you manipulate objects, not strings, not timestamps nor anything.
Any object in Python have TWO string representations:
The regular representation that is used by "print", can be get using the
str()
function. It is most of the time the most common human readable format and is used to ease display. Sostr(datetime.datetime(2008, 11, 22, 19, 53, 42))
gives you'2008-11-22 19:53:42'
.The alternative representation that is used to represent the object nature (as a data). It can be get using the
repr()
function and is handy to know what kind of data your manipulating while you are developing or debugging.repr(datetime.datetime(2008, 11, 22, 19, 53, 42))
gives you'datetime.datetime(2008, 11, 22, 19, 53, 42)'
.What happened is that when you have printed the date using "print", it used
str()
so you could see a nice date string. But when you have printedmylist
, you have printed a list of objects and Python tried to represent the set of data, usingrepr()
.The How: what do you want to do with that?
Well, when you manipulate dates, keep using the date objects all long the way. They got thousand of useful methods and most of the Python API expect dates to be objects.
When you want to display them, just use
str()
. In Python, the good practice is to explicitly cast everything. So just when it's time to print, get a string representation of your date usingstr(date)
.One last thing. When you tried to print the dates, you printed
mylist
. If you want to print a date, you must print the date objects, not their container (the list).E.G, you want to print all the date in a list :
Note that in that specific case, you can even omit
str()
because print will use it for you. But it should not become a habit :-)Practical case, using your code
Advanced date formatting
Dates have a default representation, but you may want to print them in a specific format. In that case, you can get a custom string representation using the
strftime()
method.strftime()
expects a string pattern explaining how you want to format your date.E.G :
All the letter after a
"%"
represent a format for something :%d
is the day number%m
is the month number%b
is the month abbreviation%y
is the year last two digits%Y
is the all yearetc
Have a look at the official documentation, or McCutchen's quick reference you can't know them all.
Since PEP3101, every object can have its own format used automatically by the method format of any string. In the case of the datetime, the format is the same used in strftime. So you can do the same as above like this:
The advantage of this form is that you can also convert other objects at the same time.
With the introduction of Formatted string literals (since Python 3.6, 2016-12-23) this can be written as
Localization
Dates can automatically adapt to the local language and culture if you use them the right way, but it's a bit complicated. Maybe for another question on SO(Stack Overflow) ;-)
The date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.
Here is a list of the format codes with their directive and meaning.
This is what we can do with the datetime and time modules in Python
That will print out something like this: