def ifLeapYear(year): if year % 400 == 0: return True elif year % 100 == 0: return False elif year % 4 == 0: return True else: return False def daysThisYear(month, day): days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] dayCount = 0 i = 0 while i < month - 1: dayCount += days_per_month[i] i += 1 return dayCount + day def daysLastYear(month, day): days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] dayCount = 0 i = 11 if month == 12: return 32 - day while i + 1 > month: dayCount += days_per_month[i] i -= 1 return dayCount + (days_per_month[month - 1] - day) def daysBetweenDates(year1, month1, day1, year2, month2, day2): ## leap_year_count = 0 i = year1 + 1 while i < year2: if ifLeapYear(i): leap_year_count += 1 i += 1 else: i += 1 if ifLeapYear(year2) and month2 > 2: leap_year_count += 1 if ifLeapYear(year1) and month1 < 3: leap_year_count += 1 #print leap_year_count if year1 == year2: if ifLeapYear(year1) and month1 < 3 and month2 > 2: day_count = daysThisYear(month2, day2) - daysThisYear(month1, day1) + 1 return day_count else: day_count = daysThisYear(month2, day2) - daysThisYear(month1, day1) return day_count if year1 + 1 == year2: day_count = daysThisYear(month2, day2) + daysLastYear(month1, day1) + leap_year_count return day_count day_count = daysThisYear(month2, day2) + daysLastYear(month1, day1) day_count += leap_year_count + (year2-year1-1)*365 return day_count #print daysBetweenDates(2012,1,1,2012,2,28) #print daysBetweenDates(2012,1,1,2012,3,1) #print daysBetweenDates(2011,6,30,2012,6,30) #print daysBetweenDates(2011,1,1,2012,8,8) #print daysBetweenDates(1900,1,1,1999,12,31) ### Alternate:: """ def daysInMonth(m): if m == 4 or m == 6 or m == 9 or m == 11: return 30 if m == 2: return 28 return 31 def isLeapYear(y): if y % 400 == 0: return True elif y % 100 == 0: return False elif y % 4 == 0: return True else: return False def nextDay(year, month, day): if isLeapYear(year): if month == 2 and day == 29: return year, 3, 1 if day == daysInMonth(month): if month == 12: return year+1, 1, 1 if month == 2 and isLeapYear(year): return year, month, day+1 return year, month+1, 1 return year, month, day+1 def dateIsAfter(year1, month1, day1, year2, month2, day2): if year1 > year2: return True if year1 == year2: if month1 > month2: return True if month1 == month2: return day1 > day2 return False def daysBetweenDates(year1, month1, day1, year2, month2, day2): # Returns the number of days between year1/month1/day1 # and year2/month2/day2. Assumes inputs are valid dates # in Gregorian calendar. # program defensively! Add an assertion if the input is not valid! assert dateIsAfter(year2, month2, day2, year1, month1, day1) days = 0 while dateIsAfter(year2, month2, day2, year1, month1, day1): days += 1 (year1, month1, day1) = nextDay(year1, month1, day1) return days """ # Test routine def test(): test_cases = [((2012,1,1,2012,2,28), 58), ((2012,1,1,2012,3,1), 60), ((2011,6,30,2012,6,30), 366), ((2011,1,1,2012,8,8), 585 ), ((1900,1,1,1999,12,31), 36523)] for (args, answer) in test_cases: result = daysBetweenDates(*args) if result != answer: print "Test with data:", args, "failed" else: print "Test case passed!" test()
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