Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python date string to date object

Tags:

python

date

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.

like image 584
elif Avatar asked May 10 '10 15:05

elif


People also ask

How do you create a date object in Python?

To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.

How do you parse a date in Python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.


1 Answers

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) 
like image 128
SilentGhost Avatar answered Sep 21 '22 19:09

SilentGhost