def isLeapYear(year): four = year/4.00 hundred = year/100.0 four_hundred = year/400.0 if ((four%1) * 10) != 0.0: return False else: if ((hundred%1) * 10) != 0.0: return True else: if ((four_hundred%1) * 10) != 0.0: return False else: return True daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] def daysBetweenDates(y1, m1, d1, y2, m2, d2): if y1 > y2: return 'Calculation not possible! Birth date is later than current.' if y1 == y2: if m1 > m2: return 'Calculation not possible! Birth date is later than current.' if m1 == m2: if d1 > d2: return 'Calculation not possible! Birth date is later than current.' else: born = daysOfMonths[m2-1] - daysOfMonths[m1-1] return born year_count = 0 if y1+1 < y2: y1 = y1 + 1 while y1 < y2: if isLeapYear(y1) == True: days = 366 else: days = 365 year_count = year_count + days y1 = y1 + 1 born_year = 0 i = m1 if isLeapYear(y1) == True: daysOfMonths[1] = 29 else: daysOfMonths[1] = 28 while i < len(daysOfMonths): born_year = born_year + daysOfMonths[i] i = i + 1 born = born_year + (daysOfMonths[m1-1]-d1) current_year = 0 i = 0 if isLeapYear(y2) == True: daysOfMonths[1] = 29 else: daysOfMonths[1] = 28 while i < m2-1: current_year = current_year + daysOfMonths[i] i = i + 1 current = current_year + d2 return (year_count + born + current) print daysBetweenDates(2015, 6, 27, 2015, 7, 24)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question