Python datetime.strptime月份说明符似乎不起作用 [英] Python datetime.strptime month specifier doesn't seem to work
问题描述
月份格式说明符似乎不起作用。
从$ datetime import datetime
endDate = datetime。 strptime('10 3 2011','%j%m%Y')
print endDate
2011-01-10 00:00:00
endDate = datetime.strptime('21 5 1987'''%j%m%Y')
print endDate
1987-01-21 00:00:00
现在,根据手册手册:
%m =十进制数的月份[01,12]。
所以,我失去了什么,除了我拉出来的头发,试图理解为什么我的django __filter查询什么也没有返回有效!)我试过 03
和 05
无效。
版本的东西,平台,架构等:
$ python --version
Python 2.7
$ python3 --version
Python 3.1.2
$ uname -r
2.6.35.11-83.fc14.x86_64(这是Linux / Fedora 14/64位)。
你不能混合%j
与其他格式代码如%m
因为如果您在表中查看您链接的%j是一年中的日期十进制数[001,366]所以10个记者一年的10天,所以它是1月01日...
所以你只需要写:
>>> datetime.strptime('10 2011','%j%Y')
datetime.datetime(2011,1,10,0,0)
否则,如果您希望使用10作为挂载日,则应该执行以下操作:
code>>>> datetime.strptime('10 3 2011','%d%m%Y')
datetime.datetime(2011,3,10,0,0)
pre>The month format specifier doesn't seem to work.
from datetime import datetime endDate = datetime.strptime('10 3 2011', '%j %m %Y') print endDate 2011-01-10 00:00:00 endDate = datetime.strptime('21 5 1987', '%j %m %Y') print endDate 1987-01-21 00:00:00
Now, according to the manual the manual:
%m = Month as a decimal number [01,12].
So, what am I missing, other than the hair I've pulled out trying to understand why my django __filter queries return nothing (the dates going in aren't valid!)? I've tried
03
and05
to no avail.Versions of things, platform, architecture et al:
$ python --version Python 2.7 $ python3 --version Python 3.1.2 $ uname -r 2.6.35.11-83.fc14.x86_64 (that's Linux/Fedora 14/64-bit).
解决方案You can't mix the
%j
with others format code like%m
because if you look in the table that you linked %j is the Day of the year as a decimal number [001,366] so 10 correspondent to the 10 day of the year so it's 01 of January ...So you have just to write :
>>> datetime.strptime('10 2011', '%j %Y') datetime.datetime(2011, 1, 10, 0, 0)
Else if you you wanted to use 10 as the day of the mount you should do :
>>> datetime.strptime('10 3 2011', '%d %m %Y') datetime.datetime(2011, 3, 10, 0, 0)
这篇关于Python datetime.strptime月份说明符似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!