Find same date time each next weeks until a specifc date time

153 views Asked by At

Given a start date time, an end date time and a repeat until date time how do I find the same schedule each week until repeat until date time ?

For example:

start date time: 05-29-2012 07:35
end date time: 05-29-2012 10:27 
repeat until date: 06-19-2012 09:00

result would be:

06-05-2012 07:35 06-05-2012 10:27
06-12-2012 07:35 06-12-2012 10:27

Any idea how to do that in cfml ?

Here's what I've tried so far:

<cfset start_dt = "05-29-2012 07:35">
<cfset end_dt = "05-29-2012 10:27">
<cfset repeat_until = "06-19-2012 09:00">
<cfoutput>start: #start_dt#<br></cfoutput>
<cfoutput>end: #end_dt#<br></cfoutput>
<cfoutput>repeat until: #repeat_until#<br></cfoutput>
<cfset s_date='#DatePart("m", start_dt)#/#DatePart("d", start_dt)#/#DatePart("yyyy", start_dt)#'>
<cfoutput>#s_date#<br></cfoutput>
<cfset s_date = DateAdd("d", 7, s_date)>
<cfoutput>#s_date#<br></cfoutput>
1

There are 1 answers

0
nosilleg On BEST ANSWER

With your example of expected results, it looks like you don't care about the value of start_dt only of end_dt. So here is one way that you could make it work. You only need one type of loop, but I've included two so that you can choose your own preference.

<cfset start_dt = "05-29-2012 07:35">
<cfset end_dt = "05-29-2012 10:27">
<cfset repeat_until = "06-19-2012 09:00">
<cfoutput>start: #start_dt#<br></cfoutput>
<cfoutput>end: #end_dt#<br></cfoutput>
<cfoutput>repeat until: #repeat_until#<br></cfoutput>

<!--- FromTo loop --->
<cfset first_dt = DateAdd("d", 7, end_dt)>
<cfloop from="#first_dt#" to="#repeat_until#" index="current_dt" step="#CreateTimeSpan(7,0,0,0)#">
    <cfoutput>#DateFormat(current_dt, "mm-dd-yyyy")# #TimeFormat(current_dt, "HH:mm")#<br></cfoutput>
</cfloop>

<!--- Condition loop --->
<cfset current_dt = DateAdd("d", 7, end_dt)>
<cfloop condition="DateCompare(current_dt, repeat_until) LTE 0">
    <cfoutput>#DateFormat(current_dt, "mm-dd-yyyy")# #TimeFormat(current_dt, "HH:mm")#<br></cfoutput>
    <cfset current_dt = DateAdd("d", 7, current_dt)>
</cfloop>