网上看到的计算python 月份增加的解决方法
2014-09-30 19:13
316 查看
感觉还不错,分享下。
def add_month(date): """add one month to date, maybe falling to last day of month :param datetime.datetime date: the date :: >>> add_month(datetime(2014,1,31)) datetime.datetime(2014, 2, 28, 0, 0) >>> add_month(datetime(2014,12,30)) datetime.datetime(2015, 1, 30, 0, 0) """ # number of days this month month_days = calendar.monthrange(date.year, date.month)[1] candidate = date + timedelta(days=month_days) # but maybe we are a month too far if candidate.day != date.day: # go to last day of next month, # by getting one day before begin of candidate month return candidate.replace(day=1) - timedelta(days=1) else: return candidate
相关文章推荐
- 网上看到的一篇DNS正向解析失败的问题解决方法
- 实现js浮点数加、减、乘、除的精确计算(网上很多文章里的方法是不能解决所有js浮点数计算误差的)
- 实现js浮点数加、减、乘、除的精确计算(网上很多文章里的方法是不能解决所有js浮点数计算误差的)
- 让人郁闷了好久的问题,终于解决了,之前网上也看到一些解决方法,但大多没有解决问题,所以记录下来,以供参考之用(ADO Could Not Find The Specified Provider)
- python sqlobject(mysql)中文乱码解决方法
- sqlite3 jdbc、c接口、python接口处理中文时遇到的问题及其解决方法
- 在论坛中找到的几个关于Python的小问题的解决方法
- 计算机小技巧集锦 (增加你计算机的安全系数以及一些问题的解决方法)
- 使用网上流传的一个数据库连接池在Proxy.newProxyInstance处引起 java.lang.ClassCastException 问题的解决方法
- Python转码问题的解决方法
- sqlite3 jdbc、c接口、python接口处理中文时遇到的问题及其解决方法
- WinXP下网上邻居不能访问的解决方法大全
- 用python编写ASP脚本时遇到的问题,初步的解决方法,目前正在寻找更好的解决办法。
- “由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值”的解决方法
- Python转码问题的解决方法
- VMware Tools “无法计算本地文件大小。你可能没有执行该操作的权限。”的解决方法
- 网上邻居共享问题及解决方法
- 局域网-无法访问网上邻居解决方法
- 千千静听打开网上邻居的文档,断网后卡死的解决方法
- python错误TypeError: 'module' object is not callable 的解决方法