<分区>
我已经设计了一个程序来查找第 n 个工作日而不使用循环。
请就此提出您的建议-
操纵工作日的算法-
问题:找出从任意一天开始的第 n 个工作日的日期。
解决方法:
归一化到最近的星期一 -
If today(or the initial day) happens to be something other than monday, bring the day to the closest monday by simple addition or subtraction.
例如:初始日 - 10 月 17 日。这恰好是星期三。因此,通过减少 2 个日期来将这个 no monday 正常化。 现在将这 2 个日期命名为初始标准化因子。
添加这些周中的工作日数 + 周末数。
例如:要增加 10 个工作日,我们需要增加 12 天。由于 10 天有 1 周,其中仅包括 1 个星期六和 1 个星期日。 这是因为,我们正在规范化到最近的星期一。
摊销返还-
现在从结束日期开始添加初始归一化因子(对于负初始归一化)和另一个常数因子(例如,k)。 或者如果初始归一化是从星期五获得的,则加 1,这恰好是 +3。 如果开始日期在 Saturday 和 Sunday ,则视为星期一。所以这一步不需要摊销。
例如:假设初始归一化从星期三开始,则初始归一化因子为 -2。因此,将 2 添加到结束日期和常数 k。
The constant k is either 2 or 0.
常量定义-
If initial normalization factor is -3, then add 2 to the resulting date if the day before amortization is (wed,thu,fri)
If initial normalization factor is -2, then add 2 to the resulting date if the day before amortization is (thu,fri)
If initial normalization factor is -1, then add 2 to the resulting date if the day before amortization is (fri)
例子-
Find the 15th working day from Oct,17 (wednesday).
第 1 步 -
初始归一化 = -2 现在开始日期是 10 月 15 日(星期一)。
第 2 步 -
add 15 working days -
15 days => 2 weeks
weekends = 2 (2 sat, 2 sun)
so add 15 + 4 = 19 days to Oct, 15 monday.
end_date = 2, nov, Friday
步骤 3a -
end_date = end_date + initial normalization = 4, nov sunday
步骤 3b -
end_date = end_date + constant_factor = 4, nov, sunday + 2 = 6, nov (Tuesday)
交叉验证-
Add 15th working day to Oct, 17 wednesday
Oct,17 + 3 (Oct 17,18,19) + 5 (Oct 22-26) + 5 (Oct 29 - Nov 2) + 2 (Nov 5, Nov 6)
Now the answer is 6, Nov, Tuesday.
我用几个案例验证过。请分享您的建议。
拉森。