Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Days Between by ppitek40
import math
def days_diff(date1, date2):
yearsToDays1 = ytd(date1[0])
yearsToDays2 = ytd(date2[0])
monthsToDays1 = mtd(date1[1], date1[0])
monthsToDays2 = mtd(date2[1], date2[0])
if yearsToDays1 + monthsToDays1 + date1[2] > yearsToDays2 + monthsToDays2 + date2[2]:
years = yearsToDays1 - yearsToDays2
months = monthsToDays1 - monthsToDays2
days = date1[2] - date2[2]
else:
years = yearsToDays2 - yearsToDays1
months = monthsToDays2 - monthsToDays1
days = date2[2] - date1[2]
return years + months + days
def mtd(miesiace, rok):
licznik = 0
for i in range(miesiace):
if i == 1 or i == 3 or i == 5 or i == 7 or i == 8 or i == 10 or i == 12:
licznik += 31
elif i == 2:
if isLeap(rok) is True:
licznik += 29
else:
licznik += 28
else:
licznik += 30
return licznik
def ytd(lata):
licznik = 0
for i in range(lata):
if isLeap(i) is True:
licznik += 366
else:
licznik += 365
return licznik
def isLeap(year):
if year % 4 == 0:
if year % 400 == 0:
return True
if year % 100 != 0:
return True
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert days_diff((1982, 4, 19), (1982, 4, 22)) == 3
assert days_diff((2014, 1, 1), (2014, 8, 27)) == 238
assert days_diff((2014, 8, 27), (2014, 1, 1)) == 238
Dec. 23, 2017