You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现含闰年判断的日历每周起止日期计算

功能需求
  • 已知某一年1月1日对应的星期数,获取该年日历中每一周的周一与周日对应的日期
  • 计算逻辑需兼容闰年规则
现有实现代码
# 闰年标记
leap = False 
count = 0
# 星期列表
days_of_week = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
start_week = {}
# 月份与对应月天数映射
months = {"1月": 31, "2月": 28, "3月": 31, "4月": 30, "5月": 31, "6月": 30, "7月": 31, "8月": 31, "9月": 30, "10月": 31, "11月": 30, "12月": 31}

for key, value in months.items():
    print(key, '->', value)
    start_week[key] = {"start": days_of_week[0], "end": days_of_week[-1]}
    print(start_week)

if Y >= 2001 and Y <= 2100:
    if Y % 4 == 0:
        leap = True
    else:
        for w in days_of_week:
            print(f"{w} : {count}")
            count+=1
    print(leap)

内容的提问来源于stack exchange,提问作者Ana Claudia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 03:24:07