算两个日期之间月度差
import datetime
def months(str1,str2):
year1=datetime.datetime.strptime(str1[0:10],"%Y-%m-%d").year
year2=datetime.datetime.strptime(str2[0:10],"%Y-%m-%d").year
month1=datetime.datetime.strptime(str1[0:10],"%Y-%m-%d").month
month2=datetime.datetime.strptime(str2[0:10],"%Y-%m-%d").month
num=(year1-year2)*12+(month1-month2)
return num
输入months('2018-01-23 08:18:09','2017-03-21 10:19:33')
返回10

本文介绍了一种计算两个日期之间月度差的方法。通过使用Python的datetime模块解析日期并计算年份和月份的差值,从而得出两日期间的月份数差。

1万+

被折叠的 条评论
为什么被折叠?



