Possible Duplicate:
Convert Date String to DateTime Object in Python
Is there an easy way to convert the string the string 10/22/1984
into a datetime.date object?
Possible Duplicate:
Convert Date String to DateTime Object in Python
Is there an easy way to convert the string the string 10/22/1984
into a datetime.date object?
You can use datetime.strptime method for this purpose:
from datetime import datetime
dVal = datetime.strptime('10/22/1984', '%m/%d/%Y')
You can read more using the following link that describes python strptime behavior.
Yes.
>>> datetime.datetime.strptime("10/22/1984", "%m/%d/%Y")
datetime.datetime(1984, 10, 22, 0, 0)
I'm sure there are many easy ways. Here is one:
import re
import datetime
my_date = '10/22/1984'
date_components = re.compile(r'(?P<month>\d+)/(?P<day>\d+)/(?P<year>\d+)')
matched_date_components = date_components.match(my_date)
date_time_object = datetime.date(year=matched_date_components.year,
month=matched_date_components.month,
day=matched_date_components.day)
first import datetime and then try in will work.
from datetime import datetime date = datetime.strptime('10/22/1984', '%d/%m/%y')