python-判断天数

#输入某年某月某日,判断这一天是这一年的第几天?
import datetime

dtstr="20181207"
dt=datetime.datetime.strptime(dtstr,"%Y%m%d")
another_dtstr=dtstr[:4]+'0101'
another_dt=datetime.datetime.strptime(another_dtstr,"%Y%m%d")
print(int((dt-another_dt).days)+1)

结果:

340

猜你喜欢

转载自blog.csdn.net/suxiaorui/article/details/84874544