Reputation: 2202
I use dateparser.parse
to turn a string date into a datetime
object:
>>> dateparser.parse(u'22 Décembre 2010')
datetime.datetime(2010, 12, 22, 0, 0)
But now I want to create new dates with the same string format. How can I get this?
>>> get_date_pattern(u'22 Décembre 2010')
'%d %B %Y'
Edit: I'll clarify that I don't know what the string format is (I'm iterating through a list of many date strings, and for each one I want to create a new date in the same format). I'm not looking to convert a datetime object to string, I'm looking to take a formatted string and determine what that format is.
Upvotes: 15
Views: 11385
Reputation: 99
You can use a 3rd party lib dateutil.
from dateutil import parser
dt = parser.parse("06 April, 2019")
To install this, you can do:
pip install python-dateutil
Upvotes: 3
Reputation: 46965
From the datetime
documentation:
datetime.strftime(format)
Return a string representing the date and time, controlled by an explicit format string. For a complete list of formatting directives, see section
strftime()
andstrptime()
Behavior.
Upvotes: 0