Python中两个日期之间的天数[重复]

时间:2022-08-25 18:17:32

This question already has an answer here:

这个问题在这里已有答案:

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))

4 个解决方案

#1


157  

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:

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

>>> 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

#2


30  

Do you mean full calendar days, or groups of 24 hours?

你的意思是完整的日历日,或24小时的团体?

For simply 24 hours, assuming you're using Python's datetime, then the timedelta object already has a days property:

仅仅24小时,假设您正在使用Python的日期时间,那么timedelta对象已经有一个days属性:

days = (a - b).days

For calendar days, you'll need to round a down to the nearest day, and b up to the nearest day, getting rid of the partial day on either side:

对于日历日,您需要向下舍入到最近的一天,并且b到最近的一天,摆脱任何一方的部分日:

roundedA = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
roundedB = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
days = (roundedA - roundedB).days

#3


5  

Try:

尝试:

(b-a).days

I tried with b and a of type datetime.date.

我尝试使用b和类型为datetime.date的。

#4


4  

Referencing my comments on other answers. This is how I would work out the difference in days based on 24 hours and calender days. the days attribute works well for 24 hours and the function works best for calendar checks.

引用我对其他答案的评论。这就是我如何计算基于24小时和日历天数的天数差异。 days属性适用于24小时,该功能最适合日历检查。

from datetime import timedelta, datetime

def cal_days_diff(a,b):

    A = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
    B = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
    return (A - B).days

if __name__ == '__main__':

    x = datetime(2013, 06, 18, 16, 00)
    y = datetime(2013, 06, 19, 2, 00)

    print (y - x).days          # 0
    print cal_days_diff(y, x)   # 1 

    z = datetime(2013, 06, 20, 2, 00)

    print (z - x).days          # 1
    print cal_days_diff(z, x)   # 2 

#1


157  

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:

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

>>> 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

#2


30  

Do you mean full calendar days, or groups of 24 hours?

你的意思是完整的日历日,或24小时的团体?

For simply 24 hours, assuming you're using Python's datetime, then the timedelta object already has a days property:

仅仅24小时,假设您正在使用Python的日期时间,那么timedelta对象已经有一个days属性:

days = (a - b).days

For calendar days, you'll need to round a down to the nearest day, and b up to the nearest day, getting rid of the partial day on either side:

对于日历日,您需要向下舍入到最近的一天,并且b到最近的一天,摆脱任何一方的部分日:

roundedA = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
roundedB = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
days = (roundedA - roundedB).days

#3


5  

Try:

尝试:

(b-a).days

I tried with b and a of type datetime.date.

我尝试使用b和类型为datetime.date的。

#4


4  

Referencing my comments on other answers. This is how I would work out the difference in days based on 24 hours and calender days. the days attribute works well for 24 hours and the function works best for calendar checks.

引用我对其他答案的评论。这就是我如何计算基于24小时和日历天数的天数差异。 days属性适用于24小时,该功能最适合日历检查。

from datetime import timedelta, datetime

def cal_days_diff(a,b):

    A = a.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
    B = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
    return (A - B).days

if __name__ == '__main__':

    x = datetime(2013, 06, 18, 16, 00)
    y = datetime(2013, 06, 19, 2, 00)

    print (y - x).days          # 0
    print cal_days_diff(y, x)   # 1 

    z = datetime(2013, 06, 20, 2, 00)

    print (z - x).days          # 1
    print cal_days_diff(z, x)   # 2