s = "June 19, 2010"
How do I conver that to a datetime object?
s = "June 19, 2010"
How do I conver that to a datetime object?
As of python 2.5 you have the method datetime.strptime(): http://docs.python.org/library/datetime.html
dt = datetime.strptime("June 19, 2010", "%B %d, %Y")
if your locale is EN.
Use datetime.strptime
. It takes the string to convert and a format code as arguments. The format code depends on the format of the string you want to convert, of course; details are in the documentation.
For the example in the question, you could do this:
from datetime import datetime
d = datetime.strptime(s, '%B %d, %Y')
Use datetime.datetime.strptime:
>>> import datetime
>>> s = "June 19, 2010"
>>> datetime.datetime.strptime(s,"%B %d, %Y")
datetime.datetime(2010, 6, 19, 0, 0)