思路: 月的最后一天 = 下一月第一天减去一个时间就可得到
import datetime
for i in range(1, 13):
st= (datetime.datetime(2008, i, 1)).strftime("%Y-%m-%d %H:%M:%S")
if 12 == i:
ed = (datetime.datetime(2008, 12, 31)).strftime("%Y-%m-%d %H:%M:%S")
else:
ed = (datetime.datetime(2008, i + 1, 1) - datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M:%S")
print(st, ed)
获取上月的开始与结束
import datetime
year = datetime.datetime.today().year
month = datetime.datetime.today().month
ed = (datetime.datetime(year,month,1)-datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M:%S")
if month == 1:
year = year - 1
month = 13
st = datetime.datetime(year,month-1,1).strftime("%Y-%m-%d %H:%M:%S")
print(st,ed)
网友评论