python实现两个日期之间的减法
主要函数的编写
判断闰年(JudgeLeapYear)
def JudgeLeapYear(year):
if year % 4 == 0 and year % 100 != 0:
return True
elif year % 100 == 0 and year % 400 == 0:
return True
else:
return False
判断某年中的某月有多少天(DivideMonth)
#按月份进行分类
Type_One = {1, 3, 5, 7, 8, 10, 12}
Type_Two = {4, 6, 9, 11}
#因为闰年2月份有29天,所以需要传入年份的信息,让代码判断该年是否为闰年
def DivideMonth(year, month):
if month in Type_One:
return 31
elif month in Type_Two:
return 30
else:
if JudgeLeapYear(year):
return 29
else:
return 28
计算某两年之间过了多少天(CountDifferenceInYear)
def CountDifferenceInYear(year_1, year_2):
day = 0
#只计算两个年份之间相差的天数,为主要函数服务
for year in range(min(year_1, year_2), max(year_1, year_2)):
if JudgeLeapYear(year):
day += 366
else:
day += 365
return day
计算某一年过了几天(CountDaiesInMonth)
def CountDaiesInMonth(year, month, day):
daies = 0
for i in range(1, month):
daies += DivideMonth(year, i)
return daies + day
计算两个日期之间相差的天数(DifferenceInYears)
#传进来的参数为整型列表
def DifferenceInYears(FirstYear: list, SecondYear: list):
day_one = day_two = 0
FirstYear_Count = CountDaiesInMonth(FirstYear[0], FirstYear[1], FirstYear[2])
SecondYear_Count = CountDaiesInMonth(SecondYear[0], SecondYear[1], SecondYear[2])
if FirstYear[0] <= SecondYear[0]:
day_one += CountDifferenceInYear(FirstYear[0], SecondYear[0])
day_one += SecondYear_Count
day_two += FirstYear_Count
else:
day_one += CountDifferenceInYear(SecondYear[0], FirstYear[0])
day_one += FirstYear_Count
day_two += SecondYear_Count
return day_one - day_two
完整代码
# coding=utf-8
# @Time : 2021/5/12 21:39
# @Authur : 林嚣张
# @File : practice_2.py
# @Software : PyCharm
# 输入两次日期,计算日期之间的日数
def main():
list1 = [1997, 3, 20]
list2 = [1998, 2, 4]
print(DifferenceInYears(list1, list2))
# 给月份归类
Type_One = {1, 3, 5, 7, 8, 10, 12}
Type_Two = {4, 6, 9, 11}
def DivideMonth(year, month):
if month in Type_One:
return 31
elif month in Type_Two:
return 30
else:
if JudgeLeapYear(year):
return 29
else:
return 28
# 判断是否为闰年:
def JudgeLeapYear(year):
if year % 4 == 0 and year % 100 != 0:
return True
elif year % 100 == 0 and year % 400 == 0:
return True
else:
return False
# 计算日期
def DifferenceInYears(FirstYear: list, SecondYear: list):
day_one = day_two = 0
FirstYear_Count = CountDaiesInMonth(FirstYear[0], FirstYear[1], FirstYear[2])
SecondYear_Count = CountDaiesInMonth(SecondYear[0], SecondYear[1], SecondYear[2])
if FirstYear[0] <= SecondYear[0]:
day_one += CountDifferenceInYear(FirstYear[0], SecondYear[0])
day_one += SecondYear_Count
day_two += FirstYear_Count
else:
day_one += CountDifferenceInYear(SecondYear[0], FirstYear[0])
day_one += FirstYear_Count
day_two += SecondYear_Count
return day_one - day_two
# 计算年份之间的差距
def CountDifferenceInYear(year_1, year_2):
day = 0
for year in range(min(year_1, year_2), max(year_1, year_2)):
if JudgeLeapYear(year):
day += 366
else:
day += 365
return day
# 计算某年过了多少天
def CountDaiesInMonth(year, month, day):
daies = 0
for i in range(1, month):
daies += DivideMonth(year, i)
return daies + day
if __name__ == "__main__":
main()
后记
本次练习只为了锻炼代码编写能力,具体功能的实现可以使用datetime库调用相应函数,不必重复造轮子。