python中如何获取每个月的第一天和最后一天?
2 回答
wolvvi
TA贡献18条经验 获得超9个赞
import calendar
import datetime
Format="%d-%d-%d"
year=datetime.datetime.now().year
for i in range(1,13):
d =calendar.monthrange(year,i)
print(Format % (year, i, 1))
print(Format % (year, i, d[1]))
孤独的小猪
TA贡献232条经验 获得超302个赞
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
添加回答
举报
0/150
提交
取消