"""
日历的前提是
已知1900年1月1日是星期一 按照时间向后推 出现现在的日历
"""
# 判断年是否是闰年
def is_leap(year):
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
# 获取指定月的天数
def get_days(year, month):
if month in (1,3,5,7,8,10,12):
return 31
elif month in (4,6,9,11):
return 30
else:
# 因为判断是否是闰年的功能已经存在了 所以可以直接调用
return 29 if is_leap(year) else 28
# 获取指定年到1900经历的天数
def get_year_days(year):
year_days = 0 # 记录天数
for y in range(1900, year):
year_days += 366 if is_leap(y) else 365
return year_days
# 获取当前指定日期到 当前1月经历的天数
def get_month_days(year, month):
month_days = 0 # 记录天数
for m in range(1, month):
month_days += get_days(year, m)
return month_days
# 默认date接受的时间格式为"年-月-日"
def get_week(date):
# 按照横线切割
date_list = date.split("-")
print(date_list)
date_list = [int(ele) for ele in date_list]
print(date_list)
year, month, day = date_list # 取出年月日
# 获取年到1900年经历的天数
year_days = get_year_days(year)
# 获取指定月到当前年1月的天数
month_days = get_month_days(year, month)
# 统计到具体日期的天数
days = year_days + month_days + day
# 对7 整除 求星期几
week = days % 7
if week == 0:
return '星期日'
elif week == 1:
return '星期一'
elif week == 2:
return '星期二'
elif week == 3:
return '星期三'
elif week == 4:
return '星期四'
elif week == 5:
return '星期五'
else:
return '星期六'
week = get_week("2020-02-29")
print(week)