导入datetime库(用来计算年月日的一种库)
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():
year_month_day = input('请输入(yyyy/mm/dd):')
YMD = datetime.strptime(year_month_day, '%Y/%m/%d')
print(YMD)
year = YMD.year #计算出的年份
month = YMD.month #计算出的月份
day = YMD.day #计算出当月的第几天
# 初始化天数
days = 0
days += day
# 月份集合
_30_month = {4, 6, 9, 11}
_31_month = {1, 3, 5, 7, 8, 10, 12}
for i in range(1, month):
if i in _30_month :
days += 30
elif i in _31_month :
days += 31
else:
days += 28
# 判断闰年
if is_leap_year(year) and month > 2:
days += 1
print('这是{}年的第{}天'.format(year,days))
if __name__ == '__main__':
main()