多少个日历天已经过自给定日期?

huangapple go评论68阅读模式
英文:

How many calendar days elapsed since given date?

问题

我需要找出自指定日期以来经过了多少个日历日。当我使用以下方法时,即使日期是今天,它也会给我返回-1:

>>> from datetime import datetime
>>> t = datetime.fromisoformat('2023-03-09 08:55:11')
>>> (t - datetime.today()).days
-1

然而,正确的答案应该是0。

如果我在00:00:01时询问,而日期是昨天的23:59:59,它应该返回1天。但是对于任何今天的日期,答案都应该是0。

我该如何做到这一点?

英文:

I need to find how many calendar days elapsed sice given date. When I use the following, it gives me -1 even when the date is today:

>>> from datetime import datetime
>>> t = datetime.fromisoformat('2023-03-09 08:55:11')
>>> (t - datetime.today()).days
-1

Whereas the correct answer should be 0.

If I ask at 00:00:01, and the date was yesterday at 23:59:59, it should give me 1 day. But anything today should be 0.

How can I do that?

答案1

得分: 1

将这两个值都转换为日期,而不是日期时间。

就像将0.9与1.1进行比较,希望结果为1一样。首先将它们都转换为整数将给出0和1,差值为1。

因此,比较日期,而不是日期时间...

from datetime import datetime, date

x = datetime.fromisoformat('2023-03-09 08:55:11')
y = datetime.fromisoformat('2023-03-10 07:00:00')

print((y - x).days)
print((y.date() - x.date()).days)
print()

y = datetime.fromisoformat('2023-03-10 09:00:00')
print((y - x).days)
print((y.date() - x.date()).days)

演示链接:https://trinket.io/python3/013ac6c02a

英文:

Convert both values to dates instead of datetimes.

It's like comparing 0.9 to 1.1 and wanting the result to be 1. Converting them both to integers first would give 0 and 1 with a difference of 1.

So, compare dates, not datetimes...

from datetime import datetime, date

x = datetime.fromisoformat('2023-03-09 08:55:11')
y = datetime.fromisoformat('2023-03-10 07:00:00')

print((y - x).days)
print((y.date() - x.date()).days)
print()

y = datetime.fromisoformat('2023-03-10 09:00:00')
print((y - x).days)
print((y.date() - x.date()).days)

Demo : https://trinket.io/python3/013ac6c02a

答案2

得分: 0

from datetime import datetime, date

given_date = datetime.fromisoformat('2023-03-09 08:55:11')
today = datetime.now().date() # 获取当前日期,不包括时间
elapsed_days = (today - given_date.date()).days

print(elapsed_days)
英文:
from datetime import datetime, date

given_date = datetime.fromisoformat('2023-03-09 08:55:11')
today = datetime.now().date() # get current date without time
elapsed_days = (today - given_date.date()).days

print(elapsed_days)

答案3

得分: -3

你得到负数结果的原因是你从一个未来日期减去当前日期。你可以使用math.abs,或者只需:

t= datetime.fromisoformat('2023-03-01')
elapsed_days = (datetime.today() - t).days
print(elapsed_days)
英文:

The reason you are getting a negative result is that you are subtracting the current date from a future date. You can use math.abs, or just:

t= datetime.fromisoformat('2023-03-01')
elapsed_days = (datetime.today() - t).days
print(elapsed_days)



</details>



huangapple
  • 本文由 发表于 2023年3月9日 19:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定