Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

check the input format of a date python

>>> import datetime
>>> def validate(my_str_date):
    try:
        datetime.datetime.strptime(my_str_date, '%Y-%m-%d')
    except ValueError:
        raise ValueError("Incorrect data format, should be YYYY-MM-DD")
        

>>> validate('2003-12-23')
>>> validate('2003/12/23')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in validate
ValueError: "Incorrect data format, should be YYYY-MM-DD"
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #check #input #format #date #python
ADD COMMENT
Topic
Name
3+1 =