tags:

views:

167

answers:

3

How do I convert a string to a date object in python?

The string would be: "24052010" (corresponding to the format: "%d%m%Y")

I don't want a datetime.datetime object, but rather a datetime.date

I suspect that I'm asking a trivial question but I searched and couldn't find it neither on stackoverflow nor on google.

+4  A: 
import datetime
datetime.datetime.strptime('24052010', '%d%m%Y').date()
ThiefMaster
+5  A: 
>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
SilentGhost
Oh ok. That's why I couldn't find it. :) I'm ashamed. Thank you.
elif
A: 

What do you mean by "date"? If you just want the numerical split,

date = '24052010'
day, month, year = date[:2], date[2:4], date[4:]

should do fine.

msw
I meant the datetime.date object in python.
elif