天天看点

函数判断输入日期是这年的第几天

rmonth = [0, 31, 29, 31, 30, 31, 30, 31,31, 30, 31, 30, 31]
month = [0, 31, 28, 31, 30, 31, 30, 31,31, 30, 31, 30, 31]
days = 0
def  pd_days(y, m, d):
    global rmonth, month, days
    # 判断闰年
    if y % 4 == 0 and y % 100 != 0 or y % 400 == 0:
        print('%s年是闰年' % y)
        # 判断月
        if m in range(1,13):
            # 判断日
            if d <= rmonth[m]:
                for i in range(m):
                    days += rmonth[i]
                days += d
            else:
                print('日错误')
        else:
            print('月错误')
    else:
        print('%s年不是闰年' % y)
        # 判断月
        if m in range(1, 13):
            # 判断日
            if d <= month[m]:
                for i in range(m):
                    days += month[i]
                days += d
            else:
                print('日错误')
        else:
            print('月错误')
    return days

day = input('请输入八位年月日(如20080808):')
y = int(day[:4])
m = int(day[4:6])
d = int(day[6:])
print('您输入的是 %d 年 %d 月 %d 日' % (y, m, d))
print('此日是%d年的第%d天!' % (y, pd_days(y, m, d)))