如何检查日期是否在某个间隔python中?

时间:2016-10-04 13:41:21

标签: python datetime

我正在从雅虎财务中导入日期,并希望以某种格式对其进行转换,以便我可以将它们与今天进行比较,以检查日期是否在3到9个月之后。

这是我到目前为止所做的:

<div class="row blog-post">
    {% for obj in object_list %}
        <div class="col-sm-3">
            <div class="thumbnail">
                {% if obj.image %}
                    <img src="{{ obj.image.url }}" class="img-responsive"/>
                {% endif %}
                <div class="caption post-detail-item">
                    {% if obj.draft %}<h3>Staff only: <span style="color:red;">Draft</span></h3> {% endif %} {% if obj.publish > today %}<h3>Staff only: <span style="color:red;">Future Post</span></h3>{% endif %}
                    <h3><a href='{{ obj.get_absolute_url }}'>{{ obj.title }}</a><small> {{ obj.publish|timesince }}</small></h3>
                    {% if obj.user.get_full_name %}
                        <p>Author: {{ obj.user.get_full_name }}</p>
                    {% endif %}
                    {{ obj.get_markdown|truncatechars_html:350 }}
                    <p><a href="{{ obj.get_absolute_url }}" class="btn btn-primary" type="button">View</a></p>
                </div>
            </div>
        </div>
    {% cycle "" "<div class='col-sm-12'><hr/></div></div><div></div><div class='row'>" %}
    {% endfor %}
</div>

以下是opt [“Expiry”]

的内容
0      Jan 20, 2017
1      Jan 20, 2017
2      Jan 20, 2017
3      Jan 20, 2017
4      Jan 20, 2017
5      Jan 20, 2017
6      Jan 20, 2017
7      Jan 20, 2017
8      Jan 20, 2017
9      Jan 20, 2017
10     Jan 20, 2017
11     Jan 20, 2017
12     Jan 20, 2017
13     Jan 20, 2017
14     Jan 20, 2017
15     Jan 20, 2017
16     Jan 20, 2017
17     Jan 19, 2018
18     Jan 20, 2017
19     Jan 19, 2018
20     Jan 20, 2017
21     Mar 17, 2017
22     Jan 20, 2017
23     Mar 17, 2017
24     Jan 20, 2017
25     Mar 17, 2017
26     Apr 21, 2017
27     Jun 16, 2017
28     Jan 19, 2018
29     Jan 20, 2017
           ...     
432    Jan 20, 2017
433    Jan 19, 2018
434    Oct 21, 2016
435    Jan 20, 2017
436    Jan 19, 2018
437    Oct 21, 2016
438    Jan 20, 2017
439    Jan 19, 2018
440    Oct 21, 2016
441    Jan 20, 2017
442    Jan 19, 2018
443    Oct 21, 2016
444    Jan 20, 2017
445    Jan 19, 2018
446    Oct 21, 2016
447    Jan 20, 2017
448    Oct 21, 2016
449    Jan 20, 2017
450    Oct 21, 2016
451    Jan 20, 2017
452    Oct 21, 2016
453    Jan 20, 2017
454    Oct 21, 2016
455    Jan 20, 2017
456    Oct 21, 2016
457    Jan 20, 2017
458    Jan 20, 2017
459    Jan 20, 2017
460    Jan 20, 2017
461    Jan 20, 2017

似乎我有相同的日期格式,即“%Y-%m-%d”,但我仍然没有过滤掉任何值。所有这些都是真实的,在区间内。

1 个答案:

答案 0 :(得分:1)

您正在使用transf_date = datetime.datetime.strptime(opt["Expiry"][1],'%b %d, %Y')代替transf_date = datetime.datetime.strptime(opt["Expiry"][i],'%b %d, %Y'),这意味着即使您在整个opt["Expiry"]上进行迭代,您也始终会对其进行处理条目。