Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Days Between by nsirons
# migrated from python 2.7
def days_diff(date1, date2):
"""
Find absolute diff in days between dates
"""
month = [31,28,31,30,31,30,31,31,30,31 ,30,31]
day1 = date1[0]*365+sum(month[0:date1[1]-1])+date1[2]
day2 = date2[0]*365+sum(month[0:date2[1]-1])+date2[2]
print(date1, sum(month[0:date1[1]])+date1[2])
for year in range(date1[0]):
if (year %4 ==0 and year%100 != 0) or year%400 == 0:
day1 += 1
for year in range(date2[0]):
if (year %4 ==0 and year%100 != 0) or year%400 == 0:
day2 += 1
if ((date1[0] %4 ==0 and date1[0]%100 != 0) or date1[0]%400 == 0) and date1[1] >=3:
day1 +=1
if ((date2[0] %4 ==0 and date2[0]%100 != 0) or date2[0]%400 == 0) and date2[1] >=3:
day2 +=1
return abs(day1-day2)
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
assert days_diff((7015,1,11), (8992,2,21)) == 722126
June 8, 2016
Comments: