程序:
from datetime import datetime
def is_leap_year(year): #判断是否为闰年
is_leap=False
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
is_leap=True
return is_leap
def main():
input_date=input('请输入日期(yyyy/mm/dd):')
date=datetime.strptime(input_date,'%Y/%m/%d')
print(date)
year=date.year
month=date.month
day=date.day
month_30days={4,6,9,11}
month_31days={1,3,5,7,8,10,12}
days=0
days+=day
for i in range(1,month):
if i in month_30days:
days+=30
elif i in month_31days:
days+=31
else:
days+=28
if is_leap_year(year) and month>2:
days+=1
print('这是{}年的第{}天'.format(year,days))
if __name__=='__main__':
main()
运行结果: