Get number of incidents based on date range and set days of the week

I am trying to calculate the total number of events that will occur based on the following known parameters

  • the date of the beginning
  • expiration date
  • Days of the week
  • frequency of weeks (every week, every 2 weeks, etc.).

For example, based on the following example data:

  • 01/05/2017
  • 05/31/2017
  • 1.4 (Sunday, Wednesday)
  • Every 2 weeks

I would have to calculate that this event would work four times (5/7, 5/10, 5/21, 5/24)

Below is the skeleton. I am completely obsessed with how to increase the current day in a cycle based on the number of weeks.

<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>
+4
source share
1 answer

. , .

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
+1

Source: https://habr.com/ru/post/1676760/


All Articles