pyhton 获得前几个月
#! /usr/bin/python# -*- coding: utf-8 -*-import sysimport timeimport datetimeimport sysreload(sys)sys.setdefaultencoding("utf-8")def getLastMonth(day = None, months=1): if day: today = day else: today = datetime.date.today() y = int(months/12) m = months%12 if today.month - m <= 0: y = y + 1 m = 12 + (today.month-m) nextMonth = today.replace(year=today.year-y, month=m, day=1) elif m == 0: nextMonth = today.replace(year=today.year-y, day=1) else: nextMonth = today.replace(year=today.year-y, month=today.month-m, day=1) return nextMonthd = datetime.date.today()d=getLastMonth()print d.strftime("%Y-%m-01")d = datetime.date.today()for i in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15): d=getLastMonth(day = d, months=11) print d.strftime("%Y-%m-01")print "............................."d = datetime.date(2011, 1, 1)for i in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15): d=getLastMonth(day = d, months=11) print d.strftime("%Y-%m-01")?