0

I have a list of date time strings like this.

16-Aug-2019

I want to convert the string to 2019-08-01 this date format, and I have tried on this code , but it's getting me an error.

formatd_date = datetime.strptime(formatd_date, '%y-%m-%d')

ValueError: time data 'As-of' does not match format '%y-%m-%d'

If any can help, it will be huge thank.

2

2 Answers 2

2

Convert to datetime format and then convert to string format you want to:

>>> from datetime import datetime
>>> a = "16-Aug-2019"
>>> datetime.strptime(a, "%d-%b-%Y").strftime("%Y-%m-%d")
'2019-08-16'

Documentation: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

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

1 Comment

Thank you for saving me. It's working properly @subhrajyoti-das
1

Just fails because %y is 2-digit year. Use %Y for 4-digit year.

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.