0

I have a date stored in a string as:

date = '26 March 2018'

I'm trying to convert it to a datetime object with the following code:

date_element2 = datetime.strptime(date, '%d, %B, %Y')

But I'm getting the following traceback error:

ValueError: time data '26 March 2018' does not match format '%d, %B, %Y'

I can't figure out for the life of me how the formatting is incorrect, any help would be appreciated.

Thanks,

2
  • Remove coma in your pattern Commented Mar 27, 2018 at 9:45
  • '%d, %B, %Y' should be like '%d %B %Y' Commented Mar 27, 2018 at 9:46

1 Answer 1

2

You need to remove comma , from the format string, then it should work. For example:

>>> from datetime import datetime
>>> date = '26 March 2018'

#                              v  v  No comma here
>>> datetime.strptime(date, '%d %B %Y')
datetime.datetime(2018, 3, 26, 0, 0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.