获取两个日期之间的月份

获取两个日期之间的月份

import datetime
# 获取两个日期之间的月份
def get_month_range(start_day,end_day):
    months = (end_day.year - start_day.year)*12 + end_day.month - start_day.month
    month_range = ['%s-%s' % (start_day.year + mon//12, mon % 12+1) for mon in range(start_day.month-1, start_day.month + months)]
    print(month_range)
    return month_range

get_month_range(datetime.date(2012,1,1),datetime.datetime.today())
image.png

你可能感兴趣的:(获取两个日期之间的月份)