两个日期之间的天数? [英] Days between two dates?

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

问题描述

查看两个日期之间有多少整天的最短方法是什么?
这就是我现在正在做的事情。

What's the shortest way to see how many full days have passed between two dates? Here's what I'm doing now.

math.floor((b - a).total_seconds()/float(86400))


推荐答案

假设您确实有两个日期对象,则可以从另一个对象中减去一个对象,然后查询生成的 timedelta 对象天数:

Assuming you’ve literally got two date objects, you can subtract one from the other and query the resulting timedelta object for the number of days:

>>> from datetime import date
>>> a = date(2011,11,24)
>>> b = date(2011,11,17)
>>> a-b
datetime.timedelta(7)
>>> (a-b).days
7

它也适用于日期时间-我认为它是四舍五入的直到最近的一天:

And it works with datetimes too — I think it rounds down to the nearest day:

>>> from datetime import datetime
>>> a = datetime(2011,11,24,0,0,0)
>>> b = datetime(2011,11,17,23,59,59)
>>> a-b
datetime.timedelta(6, 1)
>>> (a-b).days
6

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

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