现在我有时间:a=2015-02-23 03:56:00 b=2015-02-23 02:56:00
并且很多时间在c
如何选择此期间所涉及的时间
for d in c :
if (a >= d>= b):
print d
我的结果:
2015-02-23 13:09:00
2015-02-23 12:09:00
2015-02-23 12:09:00
结果是错误的!!!为什么?谢谢
答案 0 :(得分:0)
from datetime import datetime
start = datetime.strptime("2015-02-23 02:56:00","%Y-%m-%d %H:%M:%S")
end = datetime.strptime("2015-02-23 03:56:00","%Y-%m-%d %H:%M:%S")
if start <= d <= end:
print(d)
我还重命名了你的变量,使其更加明显,你的时间必须是&gt; =开始时间和&lt; =结束时间在范围内。
In [1]: from datetime import datetime
In [2]: start = datetime.strptime("2015-02-23 02:56:00","%Y-%m-%d %H:%M:%S")
In [3]: end = datetime.strptime("2015-02-23 03:56:00","%Y-%m-%d %H:%M:%S")
In [4]: d = datetime.strptime("2015-02-23 13:09:00","%Y-%m-%d %H:%M:%S")
In [5]: start <= d <= end
Out[5]: False
In [6]: d = datetime.strptime("2015-02-23 12:09:00","%Y-%m-%d %H:%M:%S")
In [7]: start <= d <= end
Out[7]: False
In [8]: d = datetime.strptime("2015-02-23 02:59:00","%Y-%m-%d %H:%M:%S")
In [9]: start <= d <= end
Out[9]: True