📌  相关文章
📜  python 检查字符串是否为日期格式 - Python 代码示例

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

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


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

Traceback (most recent call last):
  File "", line 1, in 
    validate('2003-12-32')
  File "", line 5, in validate
    raise ValueError("Incorrect data format, should be YYYY-MM-DD")
ValueError: Incorrect data format, should be YYYY-MM-DD