我试图根据以下已知参数计算将发生的事件总数
>开始日期
>结束日期
>星期几
>周的频率(每周,每两周等)
例如,基于以下示例数据:
> 05/01/2017
> 2017/05/31
> 1,4(周日,周三)
>每两周一次
我必须计算这个事件将运行四次(5 / 7,5 / 10,5 / 21,5 / 24)
下面是我设置的骨架.我完全不知道如何根据已经过去的周数来增加循环中的当前日期.
<cfset local.totalRuns = 0 />
<cfset local.startDate = '2017-05-01' />
<cfset local.endDate = '2017-05-31' />
<cfset local.totalDays = DateDiff("d", local.startDate, local.endDate) />
<cfset local.daysPerWeek = '1,4' />
<cfset local.recurrence = 2 />
<cfset local.currentLoop = 0 />
<cfset local.weeksToCount = local.recurrence * 2 />
<!--- Loop a day at a time --->
<cfloop from="#local.startDate#" to="#local.endDate#" index="local.thisDay" step="#createTimespan(1, 0, 0, 0)#">
<cfset local.currentDate = local.thisDay />
<!--- Loop over each allowed day of the current week and determine actual date --->
<cfloop list="#local.daysPerWeek#" index="local.currentDay">
<!--- if current date does not exceed the end date add increment (this current is incorrect) --->
<cfif DateDiff("d", local.currentDate, local.endDate) LTE 0>
<cfset local.totalRuns++ />
</cfif>
</cfloop>
<cfset local.currentLoop++ />
</cfloop>
最佳答案 这是一种格式化的评论.它显示了我将采取的方法.
set the recurrence count to 0
start looping through the list of valid days of the week (1,4) in this case
set the control date to the start date.
do something to set the control date to the earliest
date that matches the day of the week in this loop.
if the control date is greater than the end date, break out of the loop.
otherwise
add 1 to the recurrence count
set up a while loop that adds the specified number of weeks
to the control date, and repeats the check I just described
end looping through the list of valid days of the week (1,4) in this case