import datetime def main(): time1 = input() time2 = input() time1 = datetime.date(int(time1[0:4]), int(time1[4:6]), int(time1[6:])) time2 = datetime.date(int(time2[0:4]), int(time2[4:6]), int(time2[6:])) det = abs((time2-time1).days) print(det+1) if __name__ == '__main__': main()
while True: try: inp1=list(input().strip()) inp2=list(input().strip()) list1=[31,28,31,30,31,30,31,31,30,31,30,31] list2=[31,29,31,30,31,30,31,31,30,31,30,31] min_num=min(int(''.join(inp1)),int(''.join(inp2))) max_num=max(int(''.join(inp1)),int(''.join(inp2))) def isrunnian(i): if i%100!=0 and i%4==0: return True elif i%400==0: return True else: return False def data(i,info): result=0 month=int(i[4:6]) day=int(i[6:]) if info: result+=sum(list2[:month-1]) result+=day else: result+=sum(list1[:month-1]) result+=day return result if isrunnian(int(str(min_num)[0:4])): info1=True else: info1=False if isrunnian(int(str(max_num)[0:4])): info2=True else: info2=False sum1=data(str(min_num),info1) sum2=data(str(max_num),info2) min_year=int((str(min_num)[0:4])) max_year=int((str(max_num)[0:4])) for i in range(min_year,max_year): if isrunnian(i): sum2+=366 else: sum2+=365 print(sum2-sum1+1) except: break
分别对年月日进行差值计算累加天数
#coding : utf-8
year = [365, 366]
month = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]]
def isRun(y):
if y%400==0 or (y%100!=0 and y%4==0):
return 1
return 0
def count(y1, m1, d1, y2, m2, d2):
cnt = 0
for y in xrange(y1, y2):
cnt+=year[isRun(y)]
y2_isRun = isRun(y2)
for m in xrange(m1, m2):
cnt+=month[y2_isRun][m-1]
return cnt+d2-d1+1
while True:
try:
date1 = raw_input().strip()
date2 = raw_input().strip()
y1 = int(date1[0:4])
y2 = int(date2[0:4])
m1 = int(date1[4:6])
m2 = int(date2[4:6])
d1 = int(date1[6:8])
d2 = int(date2[6:8])
if(y1>y2) or (y1==y2 and m1>m2) or (y1==y2 and m1==m2 and d1>d2):
y1 = int(date2[0:4])
y2 = int(date1[0:4])
m1 = int(date2[4:6])
m2 = int(date1[4:6])
d1 = int(date2[6:8])
d2 = int(date1[6:8])
print count(y1, m1, d1, y2, m2, d2)
except:
break