0

I am trying to convert it to datetime format. but i keep getting the following error.

convert_datetime= "Jun 25, 2021 10:35:50".replace(',','')

print(datetime.strptime(convert_datetime," %b %d %Y %H:%M:%S"))

Error:

raise ValueError("time data %r does not match format %r" % ValueError: time data 'Jun 25 2021 10:35:50' does not match format ' %d %b %Y %H:%M:%S'

0

3 Answers 3

1

The error is in the formatted string you are trying to match with in the strptime function. Change

" %b %d %Y %H:%M:%S"

to

"%b %d %Y %H:%M:%S"

. The space before the %b is causing the error because you do not have a space before Jun in convert_datetime variable. Now your code modifies to:

convert_datetime= "Jun 25, 2021 10:35:50".replace(',','')
print(datetime.strptime(convert_datetime,"%b %d %Y %H:%M:%S"))

This should be working fine.

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

Comments

1
datetime.strptime(convert_datetime,"%b %d %Y %H:%M:%S")

You've an extra space at the beginning of the strp string.

Comments

0

strptime function of datetime module takes two arguments date_string and format

In your case, date_string is fine but format string is malformed. " %b %d %Y %H:%M:%S" should be "%b %d %Y %H:%M:%S".

As per documentation,

ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple. For a complete list of formatting directives, see strftime() and strptime() Behavior.

Python Official Documentation

Comments

Your Answer

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