||
输入某年某月某日,判断这一天是这一年的第几天?
import datetime
import time
dtstr = str(input('请输入日期(例:20150721):'))
dt = datetime.datetime.strptime(dtstr, "%Y%m%d")
another_dtstr =dtstr[:4] +'0101'
another_dt = datetime.datetime.strptime(another_dtstr, "%Y%m%d")
#print ("2017年已经度过:",int((dt-another_dt).days) + 1,"天")
a=dt-another_dt
print(dtstr)
print(dt)
print(another_dtstr)
print(another_dt)
print(a)
其他方法:
a=int(input("请输入年份:"))
b=int(input("请输入月份:"))
c=int(input("请输入几号:"))
b_31=[1,3,5,7,8,10,12]
b_30=[4,6,9,11]
#b_29or28=[2]
d=0
e=0
for i in range(1,b):
if i in b_31:
d+=1
elif i in b_30:
e+=1
else:
pass
if a%4==0 and b>2:
print(d*31+e*30+c+29)
elif a%4!=0 and b>2:
print(d*31+e*30+c+28)
else:
print(d*31+e*30+c)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2023-3-28 06:43
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社