问题
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