leapyear=(31,29,31,30,31,30,31,31,30,31,30,31)
normalyear=(31,28,31,30,31,30,31,31,30,31,30,31)
import time
def isleap(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
while 1:
date1 = input("请输入日期1,格式y-m-d,如:2017-11-22:")
if date1.count("-") == 2:
year1 = int(date1.split("-")[0])
month1 = int(date1.split("-")[1])
day1 = int(date1.split("-")[2])
if isleap(year1) and month1 == 2 and day1 in range(1,30):
break
elif not isleap(year1) and month1 == 2 and day1 in range(1,29):
break
elif month1 in (1,3,5,7,8,10,12) and day1 in range(1,32):
break
elif month1 in (4,6,9,11) and day1 in range(1,31):
break
else:
print("日期%s不存在!"%date1,end="")
else:
print("日期%s格式有误!"%date1,end="")
while 1:
date2 = input("请输入日期2,格式y-m-d,如:2017-11-22:")
if date2.count("-") == 2:
year2 = int(date2.split("-")[0])
month2 = int(date2.split("-")[1])
day2 = int(date2.split("-")[2])
if isleap(year2) and month2 == 2 and day2 in range(1,30):
break
elif not isleap(year2) and month2 == 2 and day2 in range(1,29):
break
elif month2 in (1,3,5,7,8,10,12) and day2 in range(1,32):
break
elif month2 in (4,6,9,11) and day2 in range(1,31):
break
else:
print("日期%s不存在!"%date2,end="")
else:
print("日期%s格式有误!"%date2,end="")
if year1 + month1 + day1 > year2 + month2 + day2:
a,b,c = year1,month1,day1
year1,month1,day1 = year2,month2,day2
year2,month2,day2=a,b,c
if date1 == date2:
print("日期%s与%s为同一天!1"%(date1,date2))
elif year1 == year2:
if month1 == month2:
print("日期%s与%s相差%d天!2"%(date1,date2,day2-day1))
else:
tmp1 = 0
tmp2 = 0
if month1 == 1 and isleap(year2):
for i in range(0,month2-1):
tmp2+=leapyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!3"%(date1,date2,tmp2-day1))
elif month1 == 1 and not isleap(year2):
for i in range(0,month2-1):
tmp2+=normalyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!4" % (date1,date2,tmp2 - day1))
elif isleap(year1):
for i in range(0,month1-1):
tmp1+=leapyear[i]
tmp1 = tmp1 + day1
for i in range(0,month2-1):
tmp2+=leapyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!5" % (date1,date2,tmp2 - tmp1))
else:
for i in range(0,month1-1):
tmp1+=normalyear[i]
tmp1 = tmp1 + day1
for i in range(0,month2-1):
tmp2+=normalyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!6" % (date1,date2,tmp2 - tmp1))
elif year2 - year1 == 1:
tmp1 = 0
tmp2 = 0
if month1 == 1 and isleap(year1):
tmp1 = 366 - day1
elif month1 == 1 and not isleap(year1):
tmp1 = 365 - day1
elif month1 != 1 and isleap(year1):
for i in range(0,month1-1):
tmp1+= leapyear[i]
tmp1 = 366 - tmp1 - day1
else:
for i in range(0,month1-1):
tmp1+= normalyear[i]
tmp1 = 365 - tmp1 - day1
if month2 == 1:
print("日期%s与日期%s相差%d天!7" % (date1,date2,tmp1 + day2))
elif isleap(year2):
for i in range(0,month2-1):
tmp2+= leapyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!8" % (date1,date2,tmp1 + tmp2))
else:
for i in range(0,month2-1):
tmp2+= normalyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!9" % (date1,date2,tmp1 + tmp2))
else:
tmp1 = 0
tmp2 = 0
tmp3 = 0
if month1 == 1 and isleap(year1):
tmp1 = 366 - day1
elif month1 == 1 and not isleap(year1):
tmp1 = 365 - day1
elif isleap(year1):
for i in range(0,month1-1):
tmp1+= leapyear[i]
tmp1 = 366 - tmp1 - day1
else:
for i in range(0,month1-1):
tmp1+= normalyear[i]
tmp1 = 365 - tmp1 - day1
for i in range(year1+1,year2):
if isleap(i):
tmp3+= 366
else:
tmp3+= 365
if month2 == 1:
print("日期%s与日期%s相差%d天!10" % (date1,date2,tmp1 + day2 + tmp3))
elif isleap(year2):
for i in range(0,month2-1):
tmp2+= leapyear[i]
tmp2 = tmp2 + day2
else:
for i in range(0,month2-1):
tmp2+= normalyear[i]
tmp2 = tmp2 + day2
print("日期%s与日期%s相差%d天!11" % (date1, date2, tmp1 + tmp2 + tmp3))
while 1:
a = input("请输入e并回车以退出!")
if a.lower() == "e":
break