Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to date type python [duplicate]

How do I convert a string to a date object in python?

The string would be: "30-01-12" (corresponding to the format: "%d-%m-%y")

I don't want a datetime.datetime object, but rather a datetime.date

like image 825
Alfredo Solís Avatar asked Mar 21 '13 20:03

Alfredo Solís


1 Answers

You still use datetime.datetime but then request just the .date() portion:

datetime.datetime.strptime('30-01-12', '%d-%m-%y').date()

Demonstration:

>>> import datetime
>>> datetime.datetime.strptime('30-01-12', '%d-%m-%y').date()
datetime.date(2012, 1, 30)
like image 113
Martijn Pieters Avatar answered Nov 10 '22 08:11

Martijn Pieters