Validate SQL formatted date
https://stackoverflow.com/questions/16870663/how-do-i-validate-a-date-string-format-in-python
def is_sql_date(str_val):
date_text = str_val.strip()
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
return date_text
#except ValueError:
# raise ValueError("Incorrect data format, should be YYYY-MM-DD")
except:
return None
### test date validation
test_date = is_sql_date('')
test_date2 = is_sql_date('2017')
test_date3 = is_sql_date('2017-07-24')
def is_sql_date(str_val):
date_text = str_val.strip()
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
return date_text
#except ValueError:
# raise ValueError("Incorrect data format, should be YYYY-MM-DD")
except:
return None
### test date validation
test_date = is_sql_date('')
test_date2 = is_sql_date('2017')
test_date3 = is_sql_date('2017-07-24')
Comments
Post a Comment