根据开始日期和每周天数数据计算结束日期



我有以下变量

INPUT
start_date = 1 JUN 2020
session_days = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
sessions_per_week_count = 7 # from above data
total sesions = 20

OUTPUT
20 JUN 2020

根据以上数据,我们可以计算出end_date应该是2018年6月20

日我需要帮助编写算法/公式,以使用基于日历的可变输入数据计算结束日期。

任何建议将不胜感激。

最后,我想出了一个非常简单的 ruby 方法

def session_dates(date, weekdays_arr, number_of_working_days)
result = []
while result.size < number_of_working_days
if weekdays_arr.include? date.wday
result << date
end
date += 1
end
result
end

通过调用这样的方法

start_date = Date.today.beginning_of_month
session_days_in_week = [0,1,2,3,4,5,6] # week starts from 0(Sunday) 
result = session_dates(start_date, session_days_in_week, 20)

我可以在输出中获取工作日的所有日期

[
[ 0] Mon, 01 Jun 2020,
[ 1] Tue, 02 Jun 2020,
[ 2] Wed, 03 Jun 2020,
[ 3] Thu, 04 Jun 2020,
[ 4] Fri, 05 Jun 2020,
[ 5] Sat, 06 Jun 2020,
[ 6] Sun, 07 Jun 2020,
[ 7] Mon, 08 Jun 2020,
[ 8] Tue, 09 Jun 2020,
[ 9] Wed, 10 Jun 2020,
[10] Thu, 11 Jun 2020,
[11] Fri, 12 Jun 2020,
[12] Sat, 13 Jun 2020,
[13] Sun, 14 Jun 2020,
[14] Mon, 15 Jun 2020,
[15] Tue, 16 Jun 2020,
[16] Wed, 17 Jun 2020,
[17] Thu, 18 Jun 2020,
[18] Fri, 19 Jun 2020,
[19] Sat, 20 Jun 2020
]

显然,结果数组中的最后一个元素是ending_date。

最新更新