I'm new to python, and have little experience using try & except statements. My code needs to check the date format entered by user. I'm currently using the code from this thread:
How do I validate a date string format in python?
but it's not working properly: i.e. the following do not raise an exception, where date_text = "20189901", as does date_text = "20181301"
import datetime
date_text = "20189901"
try:
datetime.datetime.strptime(date_text, '%Y%m%d')
except ValueError:
raise ValueError("Incorrect data format, should be YYYYMMDD")
Is there a way to check date format using a simple if statement?