from datetime import datetime a = "20220308" b = "20211001" aa = datetime.strptime(a[:4]+"-"+a[4:6]+"-"+a[6:],"%Y-%m-%d") bb = datetime.strptime(b[:4]+"-"+b[4:6]+"-"+b[6:],"%Y-%m-%d") def month_delta(start_date, end_date): flag = True if start_date > end_date: start_date, end_date = end_date, start_date flag = False year_diff = end_date.year - start_date.year end_month = year_diff * 12 + end_date.month delta = end_month - start_date.month return -delta if flag is False else delta print("---",month_delta(bb,aa))
python计算月份差
最新推荐文章于 2024-06-30 03:34:38 发布
关键词由CSDN通过智能技术生成