2

I am reading dates from csv to python in the form of 2017-01-04. I am trying to process dates like

from datetime import datetime       
day1 = datetime.strptime(date1, date_format)

where I use date_format = "%y-%m-%d"

However, when I process the data, I get the error:

time data '2017-01-04' does not match format '%y-%m-%d'

What should it be?

1
  • 3
    It's "%Y-%m-%d" upper-case Y for 4 digit years, see the docs Commented Jun 28, 2017 at 8:04

3 Answers 3

4

Y - Year with century as a decimal number.

y - Year without century as a zero-padded decimal number.

Therefore, you should use Upper Y.

For more information about the formats, you can see it all in here

Sign up to request clarification or add additional context in comments.

Comments

0

Something like :

from datetime import datetime
day1 = datetime.utcnow().strptime('2017-01-04', '%Y-%m-%d')

Comments

0

You can run it-This code run successfully and print-2017-01-04 00:00:00

     from datetime import datetime
     day1 = datetime.utcnow().strptime('2017-01-04', '%Y-%m-%d')
     print(day1)

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.