什么是正确的ISO日期格式的日期解析器格式字符串?

2024-04-25 11:17:17 发布

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

我试图用ISO日期格式解析日期,但由于我不理解的原因,我使用的模式无法工作。

从外壳:

>>> s
datetime.date(2014, 1, 3)
>>> str(s)
'2014-01-03'
>>> datetine.strptime(str(s),"%y-%m-%d").date()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\lib\_strptime.py", line 325, in _strptime
    (date_string, format))
ValueError: time data '2014-01-30' does not match format '%y-%m-$d'

但是^{cd1>}应该与^{{cd2>}匹配,对吗?为什么我会犯这个错误?


Tags: informatdatetimedate格式line模式原因
2条回答

来自docs(重点矿):

%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
%Y Year with century as a decimal number. 1970, 1988, 2001, 2013

因此,%y-%m-%d预期是{},而你有需要%Y-%m-%d的{}。在

大写和小写的区别:

>>> datetime.datetime.strptime('2014-01-03', '%Y-%m-%d').date()
datetime.date(2014, 1, 3)
>>> datetime.datetime.strptime('14-01-03', '%y-%m-%d').date()
datetime.date(2014, 1, 3)

记录在strptime and strftime behavior中。在

相关问题 更多 >