方法一
import datetime
date = input("请输入某年某月某日,格式是 yyyy-mm-dd: ")
y = int(date[0:4]) # 获取年
m = int(date[5:7]) # 获取月
d = int(date[8:]) # 获取日
targetdate = datetime.date(y,m,d) # 将输入的日期转化为标准日期
thisyeardate = datetime,date(y,1,1) # 获取当前年第一天的标准日期
daycount = (targetdate - thisyeardate + 1).days
print(daycount)
方法二
date = input("请输入某年某月某日,格式是 yyyy-mm-dd: ")
y = int(date[0:4]) # 获取年
m = int(date[5:7]) # 获取月
d = int(date[8:]) # 获取日
# 能被4整除但不能被100整除的年份为普通闰年,能被400整除的为世纪闰年。
if y%100 == 0:
if y%400 == 0:
ms = [31,29,31,30,31,30,31,31,30,31,30,31]
elif y%4 == 0:
ms = [31,29,31,30,31,30,31,31,30,31,30,31]
else:
ms = [31,28,31,30,31,30,31,31,30,31,30,31]
days = 0
for i in range(1,13): # 确定月份
if i == m :
for j in range(i-1): # 确定月份后将该月前的天数累加
days += ms[j]
print(days)