python判断当前日期是全年哪一天

该Python程序定义了一个闰年判断函数,通过用户输入日期,计算并输出给定年份中该月的天数,特别考虑了闰年的2月29日。
摘要由CSDN通过智能技术生成
'''
设计者:ISDF
版本:v3..0
日期:04/01/2019
'''
'''
设计者:ISDF
版本:v4..0
日期:03/27/2024

'''
import datetime

#闰年判断函数
def ys_leep_year(year):
    ys_leep = False
    if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
        ys_leep = True
    return  ys_leep

def main():
    input_date_string = input("请输入日期(yyyy/mm/dd):")
    date = datetime.datetime.strptime(input_date_string,"%Y/%m/%d")
    # input(date)
    year = date.year
    month = date.month
    day = date.day
    days=0
    days += day

    #计算之前当前月份的天数总和
    days_in_month_dict= {1:31,2:28,3:31,4:30,5:31,6:30,
                        7:31,8:31,9:30,10:31,11:30,12:31}
 
    if ys_leep_year(year):
        days_in_month_dict[2] = 29
    i = 1
    for i in range(1,month):
        days += days_in_month_dict[i]
    # days = sum(days_in_month)[:month - 1]) + day
    print("这是第{}天".format(days))

if __name__ == '__main__':
    main()

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISDF-工软未来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值