1>>> import datetime
2>>> def validate(date_text):
3 try:
4 datetime.datetime.strptime(date_text, '%Y-%m-%d')
5 except ValueError:
6 raise ValueError("Incorrect data format, should be YYYY-MM-DD")
7
8
9>>> validate('2003-12-23')
10>>> validate('2003-12-32')
11
12Traceback (most recent call last):
13 File "<pyshell#20>", line 1, in <module>
14 validate('2003-12-32')
15 File "<pyshell#18>", line 5, in validate
16 raise ValueError("Incorrect data format, should be YYYY-MM-DD")
17ValueError: Incorrect data format, should be YYYY-MM-DD