Python date string to date object


Question

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.

1
307
9/26/2018 11:58:47 PM

Accepted Answer

You can use strptime in the datetime package of Python:

>>> import datetime
>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
524
8/1/2019 9:52:06 PM

import datetime
datetime.datetime.strptime('24052010', '%d%m%Y').date()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon