在Python中如何获取某年中每个月的第一天和最后

2024-12-29 01:27:52
推荐回答(1个)
回答1:

方法:
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