2

I'm trying to format the time string "2016-01-01 00:00:00" to a datetime object. I tried the following code:

from datetime import datetime
a = '2016-01-01 00:00:00'
d = datetime.strptime(a,'%Y%m%d %H:%M:%S')

But I got the error message saying:

ValueError: time data '2016-01-01 00:00:00' does not match format '%Y%m%d %H:%M:%S'

What's wrong with my code? Thank you all for helping me!!!

2 Answers 2

6

- hyphens are missing in your format string:

>>> from datetime import datetime
>>> a = '2016-01-01 00:00:00'

#                Hyphens here  v  v       
>>> d = datetime.strptime(a,'%Y-%m-%d %H:%M:%S')
>>> d
datetime.datetime(2016, 1, 1, 0, 0)
Sign up to request clarification or add additional context in comments.

Comments

0

Your format string is wrong. This works:

from datetime import datetime
a = '2016-01-01 00:00:00'
d = datetime.strptime(a,'%Y-%m-%d %H:%M:%S')

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.