Python输入出生年月日计算下个生日距离今天还有多少天

#python作业练习
#老马指导

import datetime
import time

tod = datetime.datetime.now()
toyear = int(tod.strftime("%Y"))
tomon = int(tod.strftime("%m"))
todayy = int(tod.strftime("%d"))

today = tod.strftime("%Y-%m-%d")
print("今天是:"+today)

Mon = [0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def Modify_Mon(year):
    if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
        Mon[2] = 29
    else:
        Mon[2] = 28

def ins_year():
    input_year = int(input())
    if input_year <= toyear:
        Modify_Mon(input_year)
        return input_year
    else:
        print("请输入正确年份")
        return ins_year()

def ins_mon():
    input_mon = int(input())
    if year == toyear:
        limit_mon = tomon
    else:
        limit_mon = 12
        
    if input_mon >= 1 and input_mon <= limit_mon:
        return input_mon
    else:
        print("请输入正确月份")
        return input_mon()

def ins_day():
    input_day = int(input())
    if year == toyear and mon == tomon:
        limit_day = today
    else:
        limit_day = Mon[mon]
    
    if input_day >= 1 and input_day <= limit_day:
        return input_day
    else:
        print("请输入正确日期")
        return ins_day()

def how_rlong(toyear,mon,day):
    if toyear % 4 == 0 and toyear % 100 != 0 or toyear % 400 == 0:
        num = 0
        return num
    else:
        next_year = int(toyear)+1
        while(next_year % 4 != 0 or next_year % 100 == 0 and next_year % 400 != 0):
            next_year = next_year+1
        str1 = str(next_year) + "-" + str(mon) + "-" + str(day) #下一个闰年生日日期
        date1 = datetime.datetime.strptime(today[0:10],"%Y-%m-%d") #今日时间元组
        date2 = datetime.datetime.strptime(str1[0:10],"%Y-%m-%d") #下一个闰年生日时间元组
        num = (date2-date1).days
        return num
    
def how_long(toyear,mon,day):
    next_year = int(toyear) + 1 #明年年份
    str1 = str(next_year) + "-" + str(mon) + "-" + str(day) #明年生日日期
    str2 = str(int(toyear)) + "-" + str(mon) + "-" + str(day) #今年生日日期
    date1 = datetime.datetime.strptime(today[0:10],"%Y-%m-%d") #今日时间元组
    date2 = datetime.datetime.strptime(str1[0:10],"%Y-%m-%d") #明年生日时间元组
    date3 = datetime.datetime.strptime(str2[0:10],"%Y-%m-%d") #今年生日时间元组
    
    if mon == tomon:
        if day == todayy:
            num = 0
        if day > todayy:
            num = (date3-date1).days
        if day < todayy:
            num = (date2-date1).days
    elif mon < tomon:
        num = (date2-date1).days
    else:
        num = (date3-date1).days
    return num

if __name__ == "__main__":
    
    print("输入出生年份:", end = "")
    year = ins_year()
    
    print("输入月份:", end = "")
    mon = ins_mon()
    
    print("输入日期:", end = "")
    day = ins_day()
    
    myb = str(year) + "-" + str(mon) + "-" + str(day)
    print("出生日期为:" + myb)
    if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 and mon==2 and day==29:
        num = how_rlong(toyear,mon,day)
    else:
        num = how_long(toyear,mon,day)
    if num != 0:
        print("距离生日还有" + str(num) + "天")
    else:
        print("今日生日")
今天是:2023-09-21
输入出生年份:2020
输入月份:2
输入日期:29
出生日期为:2020-2-29
距离生日还有161天
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值