根据日期范围获取出现次数并设置星期几

4
我正在尝试根据以下已知参数计算事件的总数:
开始日期 结束日期 每周的天数 每几周发生一次
例如,基于以下示例数据: - 2017年5月1日 - 2017年5月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>

在本地,local.totalRuns和local.currentLoop的最终值是什么? - tech2017
1
我非常喜欢使用日期维度表进行日期范围检查。我认为这里的诀窍是要确定事件实际发生的时间。除非我误解了什么,如果一个事件每两周在星期日运行一次,如果你将开始日期设置为5/1和5/8,我认为这段代码会返回不同的结果。 - Shawn
Shawn - 你说的两周确实有点不清楚。1周将从本周开始,2周将从下周开始,其后每隔2周进行一次。初始日期有点偏差。 - justacoder
techLove - local.totalRuns 将是我返回给调用脚本的总出现次数。 - justacoder
关于日历表的问题达成一致。它们经常可以大大简化这种任务。回复:如果将startDate设置为5/1与5/8,则此代码将返回不同的结果 是的,只是我的两分钱,但是...我不确定我同意预期的结果。我希望事件从第一个星期天或星期三开始,即5/3而不是5/7,但这可能是应用程序特定规则。 - Leigh
显示剩余4条评论
1个回答

1
这是一个格式化的评论。它展示了我会采取的方法。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接