2017-05-09 73 views
4

我試圖計算時會出現基於以下已知參數事件總數的獲取發生次數

  1. 開始日期
  2. 結束日期
  3. 一週中的天
  4. 頻率周(每週,每2周,等等)

。例如,基於以下的實施例數據:

  1. 2017年5月1日
  2. 2017年5月31日
  3. 1,4(週日,週三)
  4. 每2周

我將不得不計算出此事件將運行四次(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> 
+0

你在local.totalRuns和local.currentLoop結束時會得到什麼值? – tech2017

+1

我是日期範圍檢查使用日期維度表的巨大粉絲。我也會認爲這裏的技巧是在事件實際發生時進行修復。除非我誤解了某些事情,否則如果每兩週在星期日運行,我認爲如果將startDate設置爲5/1和5/8,則此代碼將返回不同的結果。 – Shawn

+0

肖恩 - 你是對的,2周有點不清楚。本週開始1周,2周後開始下一週,之後每2周開始。這是最初的一個有點關閉。 –

回答

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