Python Datetime: All Items From Yesterday

夙愿已清 提交于 2021-02-20 19:26:26

问题


In Python, if I wanted to do a check for all items from yesterday would I do something like:

from datetime import datetime, timedelta

if datetime.datetime.today() - timedelta(days=2) < item_to_check < datetime.datetime.today():

Would this pull all items from yesterday and is this the best way to do it?


回答1:


I'd try something easier ;-)

from datetime import date, timedelta

yesterday = date.today() - timedelta(days=1)
if item_to_check.date() == yesterday:
    # yup!

Note that your:

item_to_check < datetime.datetime.today()

is true for any item that occurred before the second you called datetime.datetime.today(). My date.today() doesn't include hours, minutes or seconds (i.e., it has no "time" component).



来源:https://stackoverflow.com/questions/19851301/python-datetime-all-items-from-yesterday

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!