根据日期范围和设置星期几来获取发生次数

时间:2017-05-09 13:48:15

标签: date coldfusion iteration

我正在尝试根据以下已知参数计算可能发生的事件总数

  1. 开始日期
  2. 结束日期
  3. 一周中的几天
  4. 周的频率(每周,每两周等)
  5. 例如,基于以下示例数据:

    1. 2017年5月1日
    2. 2017年5月31日
    3. 1,4(星期日,星期三)
    4. 每两周
    5. 我必须计算这个事件会运行四次(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>
      

1 个答案:

答案 0 :(得分: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