views:

3180

answers:

5

I'm only just starting out with python so I'm still pretty clueless. What I'm trying to do is to get the date from the pc and adding it to a list. 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". I had a look at things like the str format functions but I just can't get it to do what I want.

Can anybody sheds some light on this for me.

Thanks a lot!

+1  A: 

You need to convert the date time object to a string.

The following code worked for me:

import datetime
collection = []
dateTimeString = str(datetime.date.today())
collection.append(dateTimeString)
print collection

Let me know if you need any more help.

Simon Johnson
Nice job Simon -- beat me to it by 26 seconds. :-D
HanClinto
Come on ! Don't encourage a newbie to store a string instead of a date object. He won't be able to know when it's a good or a bad idea...
e-satis
e-satis: If all you need is a string, what's the big deal? We store our firmware build dates as strings all the time -- sometimes storing a whole object is overkill when all you need is a simple timestamp (YAGNI and all).
HanClinto
Yes, in certains case it is. I just mean just a newbie won't be able to identify these cases. So let's start with the right foot :-)
e-satis
A: 

You may want to append it as a string?

import datetime 
mylist = [] 
today = str(datetime.date.today())
mylist.append(today) 
print mylist
HanClinto
A: 

You can do:

mylist.append(str(today))
Igal Serban
+4  A: 

Use date.strftime. The formatting arguments are described in the documentation.

This one is what you wanted:

some_date.strftime('%Y-%m-%d')

This one takes Locale into account. (Locality is very important!)

some_date.strftime('%c')
Ali A
+23  A: 

The WHY : dates are objects

In Python, dates are objects. Therefor, 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. So str( 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 printed mylist, you have printed a list of objects and Python tried to represent the set of data, using repr().

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 you date using str(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 :

for date in mylist :
    print str(date)

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

import datetime
mylist = []
today = datetime.date.today()
mylist.append(today)
print mylist[0] # print the date object, not the container ;-)
2008-11-22

# It's better to always use str() because :

print "This is a new day : ", mylist[0] # will work
This is a new day : 2008-11-22

print "This is a new day : " + mylist[0] # will crash
cannot concatenate 'str' and 'datetime.date' objects

print "This is a new day : " + str(mylist[0]) 
This is a new day : 2008-11-22

Advanced date formating

Dates have a default representation, but you may want to print them in a specific format. In that case, you can get a custome string representation using the strftime() method.

strftime() expects a string pattern explaining how you want to format your date.

E.G :

print today.strftime('We are the %d, %h %Y')
'We are the 22, Nov 2008'

All the letter after a "%" represent a format for something :

  • %d is the day number
  • %m is the month number
  • %y is the year last two digits
  • %Y is the all year

etc

Have a look at the doc, you can't know them all.

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 ;-)

e-satis