Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Datetime: All Items From Yesterday

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?

like image 898
user2270029 Avatar asked Mar 22 '23 18:03

user2270029


1 Answers

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).

like image 87
Tim Peters Avatar answered Mar 31 '23 16:03

Tim Peters