169

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))
3
  • 1
    It's just two dates, or do they also include time information? Nov 24, 2011 at 14:16
  • 1
    Ah — see also stackoverflow.com/questions/151199/… Nov 24, 2011 at 14:20
  • date1 = date(2018,7,3) date2 = date(2018,9,29) days = (date2-date1).days # days are already here print("Total weeks {} and days {}".format(days//7, days%7)); Jul 4, 2018 at 5:50

4 Answers 4

298

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
3
  • 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, 2011 at 10:03
  • 1
    @Bemmu: ah yes — I think timedelta doesn’t report any unit longer than days (although I could be wrong). Nov 25, 2011 at 11:28
  • 1
    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 and it worked as I expected!
    – ftoyoshima
    Nov 10, 2020 at 23:02
47

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
6
  • 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. Jun 18, 2013 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, 2013 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? Jun 20, 2013 at 13:51
  • See new answer with adjusted code. Jun 20, 2013 at 14:09
  • Sorry, you're right; I don't know why I did that. Updated.
    – DNS
    Jun 20, 2013 at 14:37
9

Try:

(b-a).days

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

6

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 
1
  • 1
    It should be (B - A).days to get the accurate diff Oct 31, 2018 at 15:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.