Search code examples
pythondatedatetimetimestampstrptime

datetime.strptime Format does not match format '%d. %B %Y'


I want to convert type of string date to other format which as dd-mm-yyyy

date_stringg='24. Juli 2020'

date_object = datetime.strptime((date_stringg), "%d. %B %Y")

print("date_object: ", date_object)

enter image description here


Solution

  • You may need to set the locale. Juli is not a valid month name in english language, I'm assuming it is Dutch.

    Assuming you want to use the Dutch locale with datetime, you can do the following

    import locale
    locale.setlocale(locale.LC_TIME, "nl_nl")
    

    After that, it will recognize Juli so this will work

    >>> date_stringg='24. Juli 2020'
    >>> 
    >>> date_object = datetime.strptime((date_stringg), "%d. %B %Y")
    >>> date_object
    datetime.datetime(2020, 7, 24, 0, 0)