如何计算两个给定日期之间的天数? [英] How to calculate number of days between two given dates?

查看:64
本文介绍了如何计算两个给定日期之间的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个日期(例如'2008/8/18''9/26/2008'),获取这两个日期之间的天数的最佳方法是什么?

If I have two dates (ex. '8/18/2008' and '9/26/2008'), what is the best way to get the number of days between these two dates?

推荐答案

如果您有两个日期对象,您可以减去它们,从而计算出 timedelta 对象。

If you have two date objects, you can just subtract them, which computes a timedelta object.

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)

文档的相关部分:
https://docs.python.org/library/datetime.html

请参见此答案作为另一个示例。

See this answer for another example.

这篇关于如何计算两个给定日期之间的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆