📜  检查日期python代码示例的输入格式

📅  最后修改于: 2022-03-11 14:45:27.080000             🧑  作者: Mango

代码示例1
>>> 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 "", line 1, in 
  File "", line 5, in validate
ValueError: "Incorrect data format, should be YYYY-MM-DD"