如何从特定日期开始月份计数?



我正在尝试创建一个存款计算器。

用户可以有两个选择,每月、每季度、每年或通过选择特定日期补充或部分提取。现在,假设用户想从2000$开始进行小额补充对于季度,下面的代码工作得很好补充:

def deposit_withdraw_relp_calc(
formula=0.0,
amount=0.0,
next_date=None,
current_date=None
):
repl_amount = 0.0
if next_date.month == current_date.month:
repl_amount = amount
formula += amount
next_date = current_date + relativedelta.relativedelta(months=repl_period)
else:
repl_amount = 0.0
return repl_amount, next_date, formula

date_start = datetime.today().date()
date_end = date_start + relativedelta.relativedelta(months=12)
next_date = date_start + relativedelta.relativedelta(months=3)
for index, dtime in enumerate(rrule(MONTHLY, dtstart=date_start, until=date_end)):
if index == 0:
print('t', 'Date', 'tt', 'Day's', 't', 'Replenishment/Partial Withdrawal', 't', '%', 'tt', 'Deposit Amount', 'tt', 'Capitalization')
X = ((P * I) / DAYS_YEAR) * 30
date = dtime.strftime("%Y-%m-%d")
P += X
percents_total += X
repl_amount, next_date, P = deposit_withdraw_relp_calc(
formula=P,
current_date=dtime,
next_date=next_date,
amount=abs(2000) if deposit_type in ['cap', 'repl'] else -abs(2000),
)
print('t', date, 't',  K, 'ttt', repl_amount, 'ttt', round(X, 2), 't', round(P, 2), 'ttt', round(X, 2))

结果如下:

Date            Day's                   Replenishment/Partial Withdrawal        %               Deposit Amount                  Capitalization
2022-06-01      30                      0.0                                     1109.59         151109.59                       1109.59
2022-07-01      31                      0.0                                     1155.06         152264.65                       1155.06
2022-08-01      31                      0.0                                     1163.89         153428.53                       1163.89
2022-09-01      30                      2000                                    1134.95         156563.48                       1134.95
2022-10-01      31                      0.0                                     1196.75         157760.23                       1196.75
2022-11-01      30                      0.0                                     1166.99         158927.22                       1166.99
2022-12-01      31                      2000                                    1214.81         162142.04                       1214.81
2023-01-01      31                      0.0                                     1239.39         163381.42                       1239.39
...

现在让我们假设用户选择了一个随机的日期比如2022-07-10要开始补货,这意味着它应该从每个月的10号开始,从用户datetime选择的1号开始,下表显示了概念证明:

Date            Day's                   Replenishment/Partial Withdrawal        %               Deposit Amount                  Capitalization
2022-06-01      30                      0.0                                     1109.59         151109.59                       1109.59
2022-07-01      31                      0.0                                     1155.06         152264.65                       1155.06
2022-07-10      31                      2000                                    1155.06         152264.65                       1155.06
2022-08-01      31                      0.0                                     1163.89         153428.53                       1163.89
2022-09-01      30                      0.0                                     1134.95         156563.48                       1134.95
2022-10-01      31                      0.0                                     1196.75         157760.23                       1196.75
2022-10-10      31                      2000                                    1196.75         157760.23                       1196.75
2022-11-01      30                      0.0                                     1166.99         158927.22                       1166.99
2022-12-01      31                      0.0                                     1214.81         162142.04                       1214.81
2023-01-01      31                      0.0                                     1239.39         163381.42                       1239.39
2023-01-10      31                      2000                                    1239.39         163381.42                       1239.39
...

问题是如何将特定的日期添加到表中,知道rrule()是每月而不是每天,在另一种形式中,创建这样一个函数的最佳方法是什么?

解决方案有点简单,我将循环从MONTHLY更改为DAILY,这样我就可以获得用户输入日期,然后我需要检查当前日期时间和用户输入的时间戳是否相同,然后我可以进行补充。

date_start = the_first_of_month()[1] 
if datetime.today().date().day != 1 
else datetime.today().date()
date_end = date_start + relativedelta.relativedelta(months=12)
user_date = datetime.strptime('2023-01-03', '%Y-%m-%d')
for index, dtime in enumerate(rrule(DAILY, dtstart=date_start, until=date_end)):
if index == 0:
print('t', 'Date', 'tt', 'Day's', 't', 'Replenishment/Partial Withdrawal', 't', '%', 'tt', 'Deposit Amount', 'tt', 'Capitalization')
if dtime.day == 1 or (dtime.day == user_date.day and dtime.timestamp() >= user_date.timestamp()):
K = count_days_in_month(dtime.year, dtime.month)
X = ((P * I) / DAYS_YEAR) * K
date = dtime.strftime("%Y-%m-%d")
P += X
percents_total += X
if dtime.timestamp() >= user_date.timestamp():
user_amount, dtime, P = deposit_withdraw_relp_calc_specific_date(
formula=P,
current_date=dtime,
next_date=user_date,
amount=abs(2000) if deposit_type in ['cap', 'repl'] else -abs(2000),
)
print('t', date, 't',  K, 'ttt', user_amount, 'ttt', round(X, 2), 't', round(P, 2), 'ttt', round(X, 2))

相关内容

  • 没有找到相关文章

最新更新