如何修复“时间数据与格式不匹配”?

2024-09-29 06:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Python连接到一个数据库

在其中一个表中,date的格式是:2019/03/02%Y-%m-%d

在Python文件中,我有:

startdate=datetime.strptime(request.form[('startdate')],'%Y-%m-%d')
enddate = datetime.strptime(request.form[('enddate')],'%Y-%m-%d')

在网页中,添加日期:2018/03/02作为startdate2019/03/02作为enddate时,出现错误:

timedata 2018/03/02 does not match format ('%Y-%m-%d').

datetime模块是用python导入的

因此,有人能解释一下如何修复这个错误吗


Tags: 文件form数据库网页datetimedaterequest格式
1条回答
网友
1楼 · 发布于 2024-09-29 06:31:03

必须使用与数据匹配的格式

正如the documentation所说:

%y Year without century as a zero-padded decimal number.
%Y Year with century as a decimal number.

没有%前缀的字符与其自身匹配

所以正确的格式应该是%Y/%m/%d

相关问题 更多 >