Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: time data 'test_str' does not match format '%m/%d/%Y'

Traceback (most recent call last):
  File "C:\pythoneg\q2.py", line 8, in <module>
    print(datetime.datetime.strptime("test_str", "%m/%d/%Y").strftime("%Y-%m-%d") )
    raise ValueError("time data %r does not match format %r" %
I tried to read the user entered date as the input and display it in the format as i desire
    import datetime
    test_str=input("Enter date")
    format= "%m/%d/%Y"
    try :
    res= bool(datetime.strptime(test_str,format))   
    except:
    print("The Date is not in the required format" )
    print(datetime.datetime.strptime("test_str", "%m/%d/%Y").strftime("%Y-%m-%d") )

I was expecting it yo give the date in the desired format

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have to make an input in the same format "%m/%d/%Y", otherwise it wont work.

Try it:

import datetime
test_str=input("Enter date")
format= "%m/%d/%Y"
try :
    res= bool(datetime.strptime(test_str,format))   
    print(datetime.datetime.strptime("test_str", "%m/%d/%Y").strftime("%Y-%m-%d") )
except:
    print("The Date is not in the required format" )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading