和标题差不多。我想在我的 Flask 应用程序中使用
datetime.strptime
以及我正在处理的发布请求的日期。
我可以直接将输入提供给
strptime
并捕获异常吗?这应该足够安全了?我之前应该做更多验证吗?
尝试捕捉是安全的
datetime.strptime
。
如果您想要更多安全性,可以使用regex
。
import re
date_pattern = r"\d{4}-\d{2}-\d{2}"
test_string = "Today is 2024-04-09,Tomorow is 2024-04-10"
dates = re.findall(date_pattern, test_string)
print("Dates:", dates)