2

I have this string

14 Mai 2014

I want to convert it to iso 8601

i read this answer and this one,

and first i try to convert string to date and then in i convert it to iso format:

test_date = datetime.strptime("14 Mai 2014", '%d %m %Y')
iso_date = test_date.isoformat()

i got this error:

ValueError: time data '14 Mai 2014' does not match format '%d %m %Y'

2 Answers 2

5

According to Python strftime reference %m means day of month and in your case "Mai" seems to be name of month in your current locale and you have to use this %b format. So your piece of code should looks like this:

test_date = datetime.strptime("14 Mai 2014", '%d %b %Y')
iso_date = test_date.isoformat()

And don't forget to set locale.

For English locale it works:

>>> from datetime import datetime
>>> test_date = datetime.strptime("14 May 2014", '%d %b %Y')
>>> print(test_date.isoformat())
2014-05-14T00:00:00
Sign up to request clarification or add additional context in comments.

4 Comments

Also, see docs.python.org/3.6/library/… for a complete list of things to put in a date format.
FYI my answer was the first one.
@dikkini yes, but the other one contains "setlocale(locale.LC_ALL, 'fr_FR')" and it's the answer exact of my question.
How do you set locale?
2

You need to use %b token instead of %m.
And for use %b token you must set a locale.
Python Documentation

import datetime
import locale

locale.setlocale(locale.LC_ALL, 'fr_FR')
test_date = datetime.datetime.strptime("14 Mai 2014", '%d %b %Y')
iso_date = test_date.isoformat()

The result will be '2014-05-14T00:00:00'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.