python datetime 加一个月_python时间模块,如何让时间一次改变一个月?

news/2024/8/26 4:35:44 标签: python datetime 加一个月

展开全部

Python语言: Python中按月增加datetime

1653#coding=utf-8

#from:

import datetime

# input datetime1, and an month offset

# return the result datetime

def datetime_offset_by_month(datetime1, n = 1):

# create a shortcut object for one day

one_day = datetime.timedelta(days = 1)

# first use div and mod to determine year cycle

q,r = divmod(datetime1.month + n, 12)

# create a datetime2

# to be the last day of the target month

datetime2 = datetime.datetime(

datetime1.year + q, r + 1, 1) - one_day

# if input date is the last day of this month

# then the output date should also be the last

# day of the target month, although the day

# may be different.

# for example:

# datetime1 = 8.31

# datetime2 = 9.30

if datetime1.month != (datetime1 + one_day).month:

return datetime2

# if datetime1 day is bigger than last day of

# target month, then, use datetime2

# for example:

# datetime1 = 10.31

# datetime2 = 11.30

if datetime1.day >= datetime2.day:

return datetime2

# then, here, we just replace datetime2's day

# with the same of datetime1, that's ok.

return datetime2.replace(day = datetime1.day)

#------------------------------------------

# test

d1 = datetime.datetime(2008, 8, 17)

d2 = datetime_offset_by_month(d1, 13)

print '2008-8-17 + 13 month'

print d2

print

d1 = datetime.datetime(2008, 8, 31)

d2 = datetime_offset_by_month(d1, 13)

print '2008-8-31 + 13 month'

print d2

print

d1 = datetime.datetime(2007, 1, 30)

d2 = datetime_offset_by_month(d1, 13)

print '2007-1-30 + 13 month'

print d2

print

result = """

2008-8-17 + 13 month

2009-09-17 00:00:00

2008-8-31 + 13 month

2009-09-30 00:00:00

2007-1-30 + 13 month

2008-02-29 00:00:00

"""

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起


http://www.niftyadmin.cn/n/927186.html

相关文章

python做一个登录注册界面_python做一个登录注册界面的方法

python做一个登录注册界面的方法 发布时间&#xff1a;2020-08-21 10:37:05 来源&#xff1a;亿速云 阅读&#xff1a;111 作者&#xff1a;小新 这篇文章主要介绍python做一个登录注册界面的方法&#xff0c;文中介绍的非常详细&#xff0c;具有一定的参考价值&#xff0c;感兴…

python的random函数_python随机模块random的22种函数(小结)

前言 随机数可以用于数学&#xff0c;游戏&#xff0c;安全等领域中&#xff0c;还经常被嵌入到算法中&#xff0c;用以提高算法效率&#xff0c;并提高程序的安全性。平时数据分析各种分布的数据构造也会用到。 random模块&#xff0c;用于生成伪随机数&#xff0c;之所以称之…

python语言关键字finally_Python中try/except/finally关键字的使用,python,tryexceptfinally

和JAVA语言一样&#xff0c;Python也用try关键字来处理异常。 为什么处理异常非常重要呢&#xff1f; 主要原因 &#xff1a;很多操作只要‘尝试’了才知道会不会成功。比如&#xff0c; 用python打开一个txt文件&#xff0c;并把txt文件中的字符串转换成整型数 。 try的搭配通…

python 提取一个单词的所有字母_如何用python提取单词(正则表达式or分割)

“What brings u here today!”&#xff08;今天什么风把你吹过来了&#xff01;&#xff09;&#xff0c;相信大家也是遇到和我一样的难题了吧&#xff0c;想把字母提取出来很简单&#xff0c;但是想把整个单词&#xff08;还不是相同的单词&#xff09;给提取出来就有点困难了…

java使用xml存储数据_「爬虫四步走」手把手教你使用Python抓取并存储网页数据

爬虫是Python的一个重要的应用&#xff0c;使用Python爬虫我们可以轻松的从互联网中抓取我们想要的数据&#xff0c;本文将基于爬取B站视频热搜榜单数据并存储为例&#xff0c;详细介绍Python爬虫的基本流程。如果你还在入门爬虫阶段或者不清楚爬虫的具体工作流程&#xff0c;那…

骁龙芯片性能排行2020_急速快讯!手机芯片性能排行榜

阅读本文前&#xff0c;请您先点击上面的“ 蓝色字体”&#xff0c;再点击“关注”&#xff0c;这样您就可以继续免费收到文章了。每天都有分享&#xff0c;完全是免费订阅&#xff0c;请放心关注。 …

vba 定义类_excel编程系列基础:认识VBA的编辑器VBE

编按&#xff1a;哈喽&#xff0c;大家好&#xff01;VBA实战入门教程第5篇&#xff0c;我们将从九九乘法表开始和结束今天的教程。之中&#xff0c;我们会认识VBE&#xff0c;也就是VBA代码的编辑器。VBE的基本概念、打开方式&#xff0c;以及它的布局和主要功能&#xff0c;它…

python与数据思维基础笔记_python数据挖掘和机器学习实战-学习笔记1

终于学完python基础、数据分析之后开始了机器学习&#xff01;开心&#xff01; 1.人工智能、机器学习、数据挖掘 人工智能是智能机器&#xff0c;如计算机所执行的与人类智能有关的活动&#xff0c;如识别、判断、证明、学习和问题求解等思维活动。即研究人类智能活动规律的一…