def daysBetweenDates(year1, month1, day1, year2, month2, day2):
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
daysOfMonthsleap = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
years = [year1]
x = year1 + 1
leapMonthsStart = 0
leapDaysStart = 0
leapMonthsEnd = 0
leapDaysEnd = 0
yearMonthsStart = 0
yearDaysStart = 0
yearMonthsEnd = 0
yearDaysEnd = 0
if year1 == year2:
years = [year1]
else:
while x <= year2:
years.append(x)
x = x + 1
def isLeapYear(n):
if n % 400 == 0:
return True
elif n % 100 == 0:
return False
elif n % 4 == 0:
return True
else:
return False
for i, year in enumerate(years):
if isLeapYear(year):
if years[i] == year1:
leapMonthsStart = sum(daysOfMonthsleap[0:month1])
leapDaysStart = day1
if years[i] == year2:
leapMonthsEnd = sum(daysOfMonthsleap[month2:12])
leapDaysEnd = day2
# print 'leap:', day2
years[i] = 366
else:
if years[i] == year1:
yearMonthsStart = sum(daysOfMonths[0:month1])
yearDaysStart = day1
if years[i] == year2:
yearMonthsEnd = sum(daysOfMonths[month2:12])
yearDaysEnd = day2
years[i] = 365
yearsDaysTotal = sum(years)
minusMonths = leapMonthsStart + yearMonthsStart + leapMonthsEnd + yearMonthsEnd
addDays = - yearDaysStart - leapDaysStart + leapDaysEnd + yearDaysEnd
answer = yearsDaysTotal - minusMonths + addDays
print 'months:', leapMonthsStart, leapMonthsEnd
print 'days:', leapDaysStart, leapDaysEnd
print 'answer:', answer, 'adding days:', addDays
#print yearMonthsStart, yearMonthsEnd
#print yearDaysStart, yearDaysEnd
#return answer
# 31, 28, 31, 30, 31, 30,
# 31, 31, 30, 31, 30, 31
daysBetweenDates(2012,1,1,2012,2,28)
我一直在考虑这个问题好几个小时,但仍然无法理解我哪里出错了。我的代码适用于这些测试用例:daysBetweenDates(2011,6,30,2012,6,30)
,daysBetweenDates(2012,1,1,2012,3,1)
或daysBetweenDates(1900,1,1,1999,12,31)
。
日期(2012,1,1,2012,2,28)
是否需要特殊条件? 31-1 + 28 = 58,我得到56
编辑:我决定在2月份添加特殊条件
if month2 == 2:
answer = yearsDaysTotal - minusMonths + addDays + 2
if month1 == 2:
answer = answer - 2
分别为2012,1,1,2012,2,28
和2012,2,1,2012,2,28
以下是完整版:
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
daysOfMonthsleap = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
years = [year1]
x = year1 + 1
leapMonthsStart = 0
leapDaysStart = 0
leapMonthsEnd = 0
leapDaysEnd = 0
yearMonthsStart = 0
yearDaysStart = 0
yearMonthsEnd = 0
yearDaysEnd = 0
if year1 == year2:
years = [year1]
else:
while x <= year2:
years.append(x)
x = x + 1
def isLeapYear(n):
if n % 400 == 0:
return True
elif n % 100 == 0:
return False
elif n % 4 == 0:
return True
else:
return False
for i, year in enumerate(years):
if isLeapYear(year):
if years[i] == year1:
leapMonthsStart = sum(daysOfMonthsleap[0:month1])
leapDaysStart = day1
if years[i] == year2:
leapMonthsEnd = sum(daysOfMonthsleap[month2:12])
leapDaysEnd = day2
# print 'leap:', day2
years[i] = 366
else:
if years[i] == year1:
yearMonthsStart = sum(daysOfMonths[0:month1])
yearDaysStart = day1
if years[i] == year2:
yearMonthsEnd = sum(daysOfMonths[month2:12])
yearDaysEnd = day2
years[i] = 365
yearsDaysTotal = sum(years)
minusMonths = leapMonthsStart + yearMonthsStart + leapMonthsEnd + yearMonthsEnd
addDays = - yearDaysStart - leapDaysStart + leapDaysEnd + yearDaysEnd
if month2 == 2:
answer = yearsDaysTotal - minusMonths + addDays + 2
if month1 == 2:
answer = answer - 2
else:
answer = yearsDaysTotal - minusMonths + addDays
print 'months:', leapMonthsStart, leapMonthsEnd
print 'days:', leapDaysStart, leapDaysEnd
print 'answer:', answer, 'adding days:', addDays
#print yearMonthsStart, yearMonthsEnd
#print yearDaysStart, yearDaysEnd
#return answer
# 31, 28, 31, 30, 31, 30,
# 31, 31, 30, 31, 30, 31
daysBetweenDates(2012,2,1,2012,2,28)