用Python计算任意一天是今年的第几天
思路:
利用数组计算好本月之前所有月份的天数,再加上本月的日数,比如:5月20日,计算的公式就为前四个月的天数(120)+本月的日数(20)= 140天。
其中要注意的是,要判断本年是否为闰年,若为闰年则2月加1。
运行如下所示:
year = int(input('year:\n'))
month = int(input('month:\n'))
day = int(input('day:\n'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0< month <= 12:
sum = months[month-1]
else:
print('data error')
sum += day
leap = 0
if(year % 400 == 0)or((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if leap == 1 and month > 2:
sum+=1
print('it is the %dth day.' % sum)
Python判断某天为本年度的第几天!!!