from datetime import datetime from datetime import timedelta import calendar def getFirstDayOfLastMonth(): d = datetime.now() c = calendar.Calendar() year = d.year month = d.month if month == 1 : month = 12 year -= 1 else : month -= 1 return datetime(year,month,1).strftime('%Y-%m-%d %X')
计算天数:
函数: calendar.monthrange(year, month) Returns weekday of first day of the month and number of days in month, for the specified year and month.
实例:
>>> import calendar >>> now_year=2012 >>> now_month=7 >>> calendar.monthrange(now_year,now_month) (6, 31) >>> calendar.monthrange(now_year,now_month)[1] 31