151

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))
martineau
  • 99,260
  • 22
  • 139
  • 249
Bemmu
  • 16,196
  • 15
  • 70
  • 92

4 Answers4

260

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
Paul D. Waite
  • 89,393
  • 53
  • 186
  • 261
  • 2
    Coolness. Somehow I always assumed "days" refers to just the day portion of the difference. So for example difference between 2010 and 2011 would be 0 days and 1 year, but turns out it does report 365 days as I wanted. – Bemmu Nov 25 '11 at 10:03
  • 1
    @Bemmu: ah yes — I think `timedelta` doesn’t report any unit longer than days (although I could be wrong). – Paul D. Waite Nov 25 '11 at 11:28
  • I was calculating the time difference between a few hours, something like 23 o'clock on day 1 and 01 o'clock on day 2. `timedelta` returned 0 in those cases, which is not what I wanted. I was looking for the date difference. As a solution, I [converted the datetime to date](https://stackoverflow.com/questions/3743222/how-do-i-convert-a-datetime-to-date) and it worked as I expected! – Filipe Toyoshima Nov 10 '20 at 23:02
42

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

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

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:

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
DNS
  • 34,791
  • 17
  • 84
  • 123
  • 3
    good spot with the difference in calender days vs 24 hours. One question is why do you add 1 day. I think the above works perfectly without it. – Matt Alcock Jun 18 '13 at 15:10
  • That's the check for calendar days. Say A is 2013-06-18 at 16:00 and B is 2013-06-19 at 02:00; one calendar day has passed, yet (B - A).days would return zero because the delta is only 10 hours. – DNS Jun 18 '13 at 19:13
  • That's the point I'm making DNS your code returns 2 because you have added another day. Without adding 1 day it returns 1. Why add a day? – Matt Alcock Jun 20 '13 at 13:51
  • See new answer with adjusted code. – Matt Alcock Jun 20 '13 at 14:09
  • Sorry, you're right; I don't know why I did that. Updated. – DNS Jun 20 '13 at 14:37
  • Round down `a` and round up `b` => For rounding up b, we need to add timedelta(days=1) as well right? `roundedB = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0)` rounds down B. `roundedB = b.replace(hour = 0, minute = 0, second = 0, microsecond = 0) + datetime.timedelta(days=1)` rounds up b. – avmohan Nov 14 '16 at 22:10
7

Try:

(b-a).days

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

Paul D. Waite
  • 89,393
  • 53
  • 186
  • 261
M S
  • 3,717
  • 3
  • 23
  • 36
5

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.

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 
Matt Alcock
  • 10,631
  • 13
  • 41
  • 59