def is_leap_year(year): """ 判断指定的年份是不是闰年

:param year: 年份
:return: 闰年返回True平年返回False
"""
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    return True
else:
    return False

def which_day(year, month, date): """ 计算指定的日期是这一年的第几天

:param year: 年
:param month: 月
:param date: 日
:return: 第几天
"""
# 平年月份天数表
_30_days_month = [4, 6, 9, 11]  # 30天的月份
_31_days_month = [1, 3, 5, 7, 8, 10, 12]  # 31天的月份

days = 0  # 定义一个变量记录天数

# 遍历月份表,累加天数
for i in range(1, month):
    if i in _30_days_month:
        days += 30
    elif i in _31_days_month:
        days += 31
    else:
        # 判断是否为闰年,计算2月天数
        if is_leap_year(year):
            days += 29
        else:
            days += 28

# 最后再加上本月的天数
days += date

return days

print(which_day(2021, 3, 1)) # 60 print(which_day(2020, 3, 1)) # 61 print(which_day(2021, 1, 1)) # 1 print(which_day(2021, 12, 31)) # 365或366

def is_leap_year(year):

原文地址: https://www.cveoy.top/t/topic/wmW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录