1. 判断从活了多少天
# -*- coding: utf-8 -*-"""
Spyder Editor
This is a temporary script file.
"""
def is_leap_year(year): #判断是否闰年
if year % 100 == 0:
if year % 400 == 0:
return True
elif year % 4 == 0:
return True
else:
return False
def dateto31(year, month, day): 判断该日期距离12.31的日子
daysofmonth_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
daysofmonth_not_leap = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap_year(year):
i = month
days = 0
while i < 12:
days = days + daysofmonth_leap[i]
i = i + 1
days = days + daysofmonth_leap[month-1] - day
else:
i = month
days = 0
while i < 12:
days = days + daysofmonth_not_leap[i]
i = i + 1
days = days + daysofmonth_not_leap[month-1] - day
return days
def onetodate(year, month, day): #判断1.1距离该日期的日子
if is_leap_year(year):
days = 366 - dateto31(year, month, day)
else:
days = 365 - dateto31(year, month, day)
return days
def in_order(year1,month1,day1,year2,month2,day2): #判断日期1是否小于日期2
if year1 > year2:
return False
elif year1 == year2:
if month1 > month2:
return False
elif month1 == month2:
if day1 > day2:
return False
else:
return True
else:
return True
else:
return True
def valid_date(year,month,day): #判断该日期是否为合法日期
daysofmonth_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
daysofmonth_not_leap = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap_year(year):
if month <= 12 and month >= 1:
if day >= 1 and day <= daysofmonth_leap[month-1]:
return True
if not is_leap_year(year):
if month <= 12 and month >= 1:
if day >= 1 and day <= daysofmonth_not_leap[month-1]:
return True
return False
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
##判断两个日期之间的日子
if not in_order(year1, month1, day1, year2, month2, day2):
return 'Undefined!'
if not valid_date(year1, month1, day1):
return 'undefined!'
if not valid_date(year2, month2, day2):
return 'undefined!'
if year1 == year2:
days = onetodate(year2, month2, day2) - onetodate(year1, month1, day1)
return days
days1 = dateto31(year1, month1, day1)
days2 = onetodate(year2, month2, day2)
days = 0
year = year1 + 1
while year < year2:
if is_leap_year(year):
days = days + 366
else:
days = days + 365
year = year + 1
days = days + days1 + days2
return days
print(daysBetweenDates(1994,10,15,2018,3,5))