可能重复:
calculate the difference between two datetime.date() dates in years and months
我希望得到两个给定日期(start_date,end_date)对象的年和月之间的相对差异。我真的不想使用第三方库来实现这一目标。目前我正在尝试的是:
import datetime
def get_time_span(start_date, end_date):
year_diff = abs(end_date.year - start_date.year)
month_diff = abs(end_date.month - start_date.month)
return u'(%s years and %s months)' % (year_diff, month_diff)