0

I'm parsing a date from a string like below:

In [26]: import datetime

In [27]: datetime.datetime.strptime("2017-02-17", "%y-%m-%d")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-800b4156e406> in <module>()
----> 1 datetime.datetime.strptime("2017-02-17", "%y-%m-%d")

/usr/lib/python2.7/_strptime.py in _strptime(data_string, format)
    323     if not found:
    324         raise ValueError("time data %r does not match format %r" %
--> 325                          (data_string, format))
    326     if len(data_string) != found.end():
    327         raise ValueError("unconverted data remains: %s" %

ValueError: time data '2017-02-17' does not match format '%y-%m-%d'

However, I'm missing something obvious and for the life of me can't figure what is it. Can someone help me parse it?

2
  • 2
    datetime.datetime.strptime("2017-02-17", "%Y-%m-%d") (capital Y) Commented Feb 10, 2017 at 9:27
  • @Jean-FrançoisFabre Thanks man!! If you want to post as an answer, post else I'll delete the post Commented Feb 10, 2017 at 9:28

1 Answer 1

2

%y is the 2-digit legacy format for years. You need %Y

datetime.datetime.strptime("2017-02-17", "%Y-%m-%d")

result:

datetime.datetime(2017, 2, 17, 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.