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") )
我期待它以所需的格式給出日期
uj5u.com熱心網友回復:
您必須以相同的格式“%m/%d/%Y”進行輸入,否則將無法正常作業。
試試看:
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" )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/530342.html
標籤:Python约会时间
