赞
踩
Python的日期时间函数中好像没有把一个日期(时间)型变量加上几个月的函数,仿照Delphi中的自己写了一个。代码如下:
import calendar
Yearmonth = ((0,31,28,31,30,31,30,31,31,30,31,30,31),
(0,31,29,31,30,31,30,31,31,30,31,30,31))
def addmonths(begindate, months): n = begindate.year*12 + begindate.month - 1 n = n + months ryear = n / 12 rmonth = n%12 + 1 rday = begindate.day if calendar.isleap(ryear): if rday > Yearmonth[1][rmonth]: rday = Yearmonth[1][rmonth] else: if rday > Yearmonth[0][rmonth]: rday = Yearmonth[0][rmonth] return begindate.replace(year=ryear, month=rmonth, day = rday)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。