...

/

Solution: Find the Number of Days between Two Date Tuples

Solution: Find the Number of Days between Two Date Tuples

Learn how to find the number of days between two date tuples.

We'll cover the following...

The solution to the problem of finding the number of days between two date tuples is given below.

Solution

Press + to interact
# Determine number of days between two dates
dt1 = (17, 3, 1998)
dt2 = (17, 4, 2011)
d1, m1, y1 = dt1[0], dt1[1], dt1[2]
d2, m2, y2 = dt2[0], dt2[1], dt2[2]
days1 = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days2 = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
ndays1 = (y1 - 1) * 365
ldays1 = ((y1 - 1) // 4) - ((y1 - 1) // 100) + ((y1 - 1) // 400)
tdays1 = ndays1 + ldays1
if((y1 % 4 == 0 and y1 % 100 != 0) or (y1 % 400 == 0)) :
days1[1] = 29
else :
days1[1] = 28
s1 = sum(days1[0:m1 - 1])
tdays1 += s1
ndays2 = (y2 - 1) * 365
ldays2 = ((y2 -1) // 4) - ((y2 - 1) // 100) + ((y2 - 1) // 400)
tdays2 = ndays2 + ldays2
if((y2 % 4 == 0 and y2 % 100 != 0) or (y2 % 400 == 0)) :
days2[1] = 29
else :
days2[1] = 28
s2 = sum(days2[0:m2 - 1])
tdays2 += s2
diff = tdays2 - tdays1
print('Difference in days = ', diff)

Explanation

  • Lines 2–3: We create two date tuples: dt1 and dt2.
  • Lines
...