您的位置:首页 >Python 生成每月15日与月末日期序列的规范方法
发布于2026-04-04 阅读(0)
扫一扫,手机访问
本文介绍如何使用 Python 稳健、可读地生成以指定起始日为起点、按“每月15日 + 月末”双频次规则递推的日期列表,适用于贷款还款、薪资发放等场景,并提供简洁可靠的实现方案。
本文介绍如何使用 Python 稳健、可读地生成以指定起始日为起点、按“每月15日 + 月末”双频次规则递推的日期列表,适用于贷款还款、薪资发放等场景,并提供简洁可靠的实现方案。
在金融与财务系统开发中,常需生成如“每月15日及当月最后一天”的交替付款日序列(即 15_EOM 模式)。该序列必须严格满足两个核心要求:
原代码逻辑复杂、分支冗余(如手动处理12月跳转、错误使用 timedelta(15) 计算月末)、且依赖未定义变量(如 pmtmonth 多处误用),导致结果混乱(含 2024-11-16、2024-12-01 等无效日期)。
✅ 正确解法应基于状态驱动+确定性规则:
以下是精简、健壮、无外部依赖(仅标准库)的实现:
from datetime import date, datetime, timedelta
def get_last_day_of_month(year: int, month: int) -> int:
"""返回指定年月的最后一天日期(如2024-04 → 30)"""
if month == 12:
next_month = date(year + 1, 1, 1)
else:
next_month = date(year, month + 1, 1)
return (next_month - timedelta(days=1)).day
def generate_15_eom_dates(
first_payment_date: datetime,
term: int,
sm_type: str = "15_EOM"
) -> list[datetime]:
"""
生成 '15_EOM' 模式的付款日期序列
Args:
first_payment_date: 首期付款日(datetime对象)
term: 总期数(整数)
sm_type: 频率类型,仅支持 "15_EOM"
Returns:
按时间顺序排列的 datetime 列表,长度为 term
"""
if sm_type != "15_EOM":
raise ValueError("Only '15_EOM' frequency is supported.")
if term < 1:
return []
dates = [first_payment_date]
current = first_payment_date.date() # 统一转为 date 类型简化操作
for _ in range(term - 1):
if current.day == 15:
# 当前是15日 → 取当月最后一天
last_day = get_last_day_of_month(current.year, current.month)
current = current.replace(day=last_day)
else:
# 当前是月末 → 取下月15日
if current.month == 12:
current = current.replace(year=current.year + 1, month=1, day=15)
else:
current = current.replace(month=current.month + 1, day=15)
dates.append(datetime.combine(current, datetime.min.time()))
return dates
# 使用示例
if __name__ == "__main__":
firstpaymentdate = datetime.strptime("3/31/2024", "%m/%d/%Y")
term = 16
result = generate_15_eom_dates(firstpaymentdate, term)
for dt in result:
print(dt.strftime("%Y-%m-%d"))输出示例(前8项):
2024-03-31 2024-04-15 2024-04-30 2024-05-15 2024-05-31 2024-06-15 2024-06-30 2024-07-15 ...
? 关键设计说明:
⚠️ 注意事项:
该方案兼顾正确性、可维护性与性能,可直接集成至生产级财务模块。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9