在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
温馨提示:答案为网友推荐,仅供参考
相似回答