Sometimes, we want to convert Python date string to date object
In this article, we’ll look at how to convert Python date string to date object.
How to convert Python date string to date object?
To convert Python date string to date object, we can use the strptime
method.
For instance, we write
import datetime
d = datetime.datetime.strptime('24052010', "%d%m%Y").date()
to call datetime.datetime.strptime
with the date string that we want to parse into a datetime object.
And we pass in a string to specify the format of the date string so that strptime
can parse it.
Then we call date
to return the date object from the parsed datetime object.
Conclusion
To convert Python date string to date object, we can use the strptime
method.