在Python中如何获取某年中每个月的第一天和最后
展开全部
方法:
import datetime
for x in xrange(1, 13):
dt_start = (datetime.datetime(2016, x, 1)).strftime("%Y%m%d")
if 12 == x:
dt_end = (datetime.datetime(2016, 12, 31)).strftime("%Y%m%d")
else:
dt_end = (datetime.datetime(2016, x+1, 1) - datetime.timedelta(days = 1)).strftime("%Y%m%d")
print dt_start, dt_end
import datetime
for x in xrange(1, 13):
dt_start = (datetime.datetime(2016, x, 1)).strftime("%Y%m%d")
if 12 == x:
dt_end = (datetime.datetime(2016, 12, 31)).strftime("%Y%m%d")
else:
dt_end = (datetime.datetime(2016, x+1, 1) - datetime.timedelta(days = 1)).strftime("%Y%m%d")
print dt_start, dt_end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询