程序:
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
days_in_month_list=[31,28,31,30,31,30,31,31,30,31,30,31]
if is_leap_year(year):
days_in_month_list[1]=29
days=sum(days_in_month_list[:month-1])+day
# if month>2 and is_leap_year(year):
# days+=1
print('这是{}年的第{}天'.format(year,days))
if __name__=='__main__':
main()
运行结果: