Shawn
Shawn

Reputation: 331

Value Error: Format does not match while converting string to datime.date?

I am converting the following string 2020-02-14 20:56:00 to datetime.date format. But am getting the following error:

ValueError: time data '2020-02-14 20:56:00' does not match format '%Y/%m/%d %H:%M:%S'

Here is the code I am using:

from datetime import datetime 
date_and_time = '2020-02-14 20:56:00'
date_and_time = datetime.strptime(date_and_time, '%Y/%m/%d %H:%M:%S')

What am I doing wrong here?

Upvotes: 0

Views: 32

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71689

The ValueError specifies the use of incorrect format string.
To resolve this error you have to replace / with -.
Here is correct code:

from datetime import datetime 
date_and_time = '2020-02-14 20:56:00'
date_and_time = datetime.strptime(date_and_time, '%Y-%m-%d %H:%M:%S')

Hope it helps you:)

Upvotes: 1

Related Questions