下一重定价日计算方法:
假设客户11月1日提交重定价周期调整,并将重定价周期调整为按季。
1)贷放日调整:假定客户贷款发放日为2020年8月9号,调整前为每年的8月9日,调整后的每年重定价日为2月9日、5月9日、8月9日和11月9日,取距离当前日期最近的下一个重定价日为2024年11月9日
from datetime import datetime, timedelta, time
from calendar import monthrange
# 根据贷款日,求季度新定价日列表
def quarter(date_str):
month, day = date_str.split("月")
month = int(month.replace("月", ""))
day = int(day.replace("日", ""))
remainder = month % 3
given_dates_str = []
for j in range(1, 13):
yushu = j % 3 # j:1,2,3......12
if (remainder == yushu):
month_str = f'{j:02d}月' # 使用格式化字符串来确保月份是两位数
if (day > 29) and (j in [2, 4, 6, 9, 11]):
_, last_day = monthrange(2025, j)
given_dates_str.append(datetime(2025, j, last_day).date().strftime("%m月%d日"))
elif (day == 29) and (j == 2):
given_dates_str.append(datetime(2025, j, 28).date().strftime("%m月%d日"))
else:
given_dates_str.append(datetime(2025, j, day).date().strftime("%m月%d日"))
return given_dates_str
# 将字符串日期转换为datetime对象,并考虑今年和明年两种情况
def parse_date_with_year(date_str, year):
# print(date_str)
month, day = date_str.split("月")
month = int(month.replace("月", ""))
day = int(day.replace("日", ""))
# 闰年2月份29天的情况
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
if month == 2 and day > 27:
day = 29
return datetime(year, month, day)
# ------------------ 输入贷款日期--------------
print('请输入贷款日期(格式例:xx月xx日):12月10日')
load_date = input('input:')
# ------------------ 获取当前日期--------------
current_date = datetime.now()
# print(f'当前日期:{datetime(current_date.year, current_date.month, current_date.day)}')
ThisYear = int(current_date.year)
NextYear = int(current_date.year) + 1
# --------------根据贷款日,求季度新定价日列表---
given_dates_str = quarter(load_date)
print(f'季度列表:{given_dates_str}')
# 初始化最小差距和最近日期
min_diff = None
closest_date = None
# 遍历给定日期列表,并考虑今年和明年两种情况
for date_str in given_dates_str:
# 今年的情况
date_ThisYear = parse_date_with_year(date_str, ThisYear)
# print(f'今年{date_ThisYear}')
if date_ThisYear > current_date:
# 如果今年的日期在当前日期之后,则计算差距并与当前最小差距比较
diff = date_ThisYear - current_date
else:
# 否则,考虑明年的情况(因为不能是过去的时间)
date_NextYear = parse_date_with_year(date_str, NextYear)
diff = date_NextYear - current_date
# print(f'明年{date_NextYear}')
# 如果这是第一个日期或者当前差距更小,则更新最小差距和最近日期
if min_diff is None or diff < min_diff:
min_diff = diff
closest_date = date_ThisYear if date_ThisYear > current_date else date_NextYear
# 输出结果
print(f"离当前日期最近的未来日期是:{closest_date.strftime('%Y-%m-%d')},差距为:{min_diff.days}天")