如何使用Python计算两个日期之间的天数? [英] How do I calculate number of days betwen two dates using Python?

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

问题描述

如果我有两个日期(例如'8/18/2008''9/26/2008')在几天内得到差异的最佳方式是什么?

If I have two dates (ex. '8/18/2008' and '9/26/2008') what is the best way to get the difference measured in days?

推荐答案

如果你有两个日期对象,你可以

If you have two date objects, you can just subtract them.

from datetime import date

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

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

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

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