Python日期时间:昨天的所有项目 [英] Python Datetime: All Items From Yesterday
本文介绍了Python日期时间:昨天的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,如果我想检查昨天的所有物品,我会做类似的事情:
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?
推荐答案
我会尝试更简单的方法;-)
I'd try something easier ;-)
from datetime import date, timedelta
yesterday = date.today() - timedelta(days=1)
if item_to_check.date() == yesterday:
# yup!
请注意,您的
item_to_check < datetime.datetime.today()
对于在您调用 datetime.datetime.today()
的第二秒之前发生的任何项目,
是正确的.我的 date.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).
这篇关于Python日期时间:昨天的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文