当timedelta.days小于1时确定python中的“天”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14635486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:00:51  来源:igfitidea点击:

Determining "days" in python when the timedelta.days is less than one

pythondatedatetimetimedelta

提问by psxndc

sorry in advance if this is dense. I am trying to find the days since I last posted a tweet. The problem I am running into is when the dates are different, e.g., today and yesterday, but not enough hours have passed to be a full "day."

如果这是密集的,请提前抱歉。我试图找到自上次发布推文以来的日子。我遇到的问题是当日期不同时,例如今天和昨天,但没有足够的时间来成为完整的“一天”。

# "created_at" is part of the Twitter API, returned as UTC time. The 
# timedelta here is to account for the fact I am on the west coast, USA 
lastTweetAt =  result.created_at + timedelta(hours=-8)

# get local time
rightNow = datetime.now()

# subtract the two datetimes (which gives me a timedelta)
dt = rightNow - lastTweetAt

# print the number of days difference
print dt.days

The problem is that if I posted a tweet at say 5 PM yesterday and am running the script at 8 AM today, only 15 hours have passed, which is 0 days. But obviously I want to say it's been 1 day since my last tweet if it was yesterday. And a kludge of adding "+1" isn't going to help because if I have tweeted today, I want the result to be 0.

问题是,如果我昨天下午 5 点发布了一条推文,今天早上 8 点运行脚本,那么只过去了 15 个小时,也就是 0 天。但显然我想说如果是昨天的话,距离我上一条推文已经过去了 1 天。加上“+1”不会有任何帮助,因为如果我今天发了推文,我希望结果为 0。

Is there a better approach than using timedelta to get the difference?

有没有比使用 timedelta 来获得差异更好的方法?



Solutionprovided by Matti Lyra

Matti Lyra 提供的解决方案

The answer is to call .date() on the datetimes so they are converted to coarser date objects (without timestamps). The correct code looks like:

答案是在日期时间调用 .date() 以便将它们转换为更粗略的日期对象(没有时间戳)。正确的代码如下所示:

# "created_at" is part of the Twitter API, returned as UTC time.
# the -8 timedelta is to account for me being on the west coast, USA
lastTweetAt =  result.created_at + timedelta(hours=-8)

# get UTC time for right now
rightNow = datetime.now()

# truncate the datetimes to date objects (which have dates, but no timestamp)
# and subtract them (which gives me a timedelta)
dt = rightNow.date() - lastTweetAt.date()

# print the number of days difference
print dt.days

采纳答案by Matti Lyra

You can use datetime.date()to compare the two dates (note: NOT dates with times), this truncates the datetimeto have an resolution of days not hours.

您可以使用datetime.date()来比较两个日期(注意:不是日期与时间),这会截断日期而datetime不是小时的分辨率。

...
# subtract the two datetimes (which gives me a timedelta)
dt = rightNow.date() - lastTweetAt.date()
...

The docs are always your friend

文档永远是你的朋友

http://docs.python.org/2/library/datetime#datetime.datetime.date

http://docs.python.org/2/library/datetime#datetime.datetime.date

回答by BorrajaX

How about dealing just with the "date" part of your datetimes?

只处理日期时间的“日期”部分怎么样?

The part after outputing "0" in the following code:

以下代码中输出“0”后的部分:

>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now() - datetime.timedelta(hours=20)
>>> (a-b).days
0
>>> b.date() - a.date()
datetime.timedelta(-1)
>>> (b.date() - a.date()).days
-1