准确获取跨越不同年份的两个日期之间的月数的最佳方法
Best way to accurately get the number of months between two dates spanning different years
我需要能够准确地找到两个日期之间的月数,即使它们跨越不同的年份。我从这个 Whosebug thread 中获得了以下解决方案,但在日期跨度不同的年份时它失败了。
跨越一年有效
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2010-12-01', '%Y-%m-%d')
diff = relativedelta(date2, date1)
number_of_months = diff.months
print(number_of_months) # 11
跨越多年时失败
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2012-02-01', '%Y-%m-%d')
diff = relativedelta(date2, date1)
number_of_months = diff.months
print(number_of_months) # 1
鉴于此,我考虑了以下解决方案:
from datetime import datetime
from dateutil.relativedelta import relativedelta
def month_diff(date1, date2):
if date1.year == date2.year:
diff = relativedelta(date2, date1)
number_of_months = diff.months
else:
num_of_days = (date2 - date1).days
number_of_months = num_of_days//30
print(number_of_months)
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2010-12-01', '%Y-%m-%d')
date3 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date4 = datetime.strptime('2012-02-01', '%Y-%m-%d')
month_diff(date1, date2) # 11
month_diff(date3, date4) # 25
因此,我的问题是,是否有比我目前拥有的更好的解决方案?
怎么样
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2012-02-01', '%Y-%m-%d')
diff = relativedelta(date2, date1)
number_of_months = 12 * diff.years + diff.months
我需要能够准确地找到两个日期之间的月数,即使它们跨越不同的年份。我从这个 Whosebug thread 中获得了以下解决方案,但在日期跨度不同的年份时它失败了。
跨越一年有效
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2010-12-01', '%Y-%m-%d')
diff = relativedelta(date2, date1)
number_of_months = diff.months
print(number_of_months) # 11
跨越多年时失败
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2012-02-01', '%Y-%m-%d')
diff = relativedelta(date2, date1)
number_of_months = diff.months
print(number_of_months) # 1
鉴于此,我考虑了以下解决方案:
from datetime import datetime
from dateutil.relativedelta import relativedelta
def month_diff(date1, date2):
if date1.year == date2.year:
diff = relativedelta(date2, date1)
number_of_months = diff.months
else:
num_of_days = (date2 - date1).days
number_of_months = num_of_days//30
print(number_of_months)
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2010-12-01', '%Y-%m-%d')
date3 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date4 = datetime.strptime('2012-02-01', '%Y-%m-%d')
month_diff(date1, date2) # 11
month_diff(date3, date4) # 25
因此,我的问题是,是否有比我目前拥有的更好的解决方案?
怎么样
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1 = datetime.strptime('2010-01-01', '%Y-%m-%d')
date2 = datetime.strptime('2012-02-01', '%Y-%m-%d')
diff = relativedelta(date2, date1)
number_of_months = 12 * diff.years + diff.months