您可以使用单行代码,使用
datetime,添加一个月(使用定义的函数),然后转换回字符串:
x = add_months(datetime.datetime(*[int(item) for item in x.split('-')]), 1).strftime("%Y-%m-%d")
>>> import datetime, calendar>>> x = "2015-01-30">>> x = add_months(datetime.datetime(*[int(item) for item in x.split('-')]), 1).strftime("%Y-%m-%d")>>> x'2015-02-28'>>>
add_months:
def add_months(sourcedate,months): month = sourcedate.month - 1 + months year = sourcedate.year + month / 12 month = month % 12 + 1 day = min(sourcedate.day,calendar.monthrange(year,month)[1]) return datetime.date(year,month,day)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)