date_str = input("请输入日期,格式为:xxxx年x月x日:")
year, month, day = date_str.split("年")[0], date_str.split("年")[1].split("月")[0], date_str.split("月")[1].split("日")[0]

year = int(year)
month = int(month)
day = int(day)

days = 0
for i in range(1, month):
    if i in [1, 3, 5, 7, 8, 10, 12]:
        days += 31
    elif i in [4, 6, 9, 11]:
        days += 30
    else:
        if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
            days += 29
        else:
            days += 28

days += day
print("该日期为本年的第{}天".format(days))

输出:

请输入日期,格式为:xxxx年x月x日:2023年2月27日
该日期为本年的第58天
``

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

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