0

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>
Stephane
  • 4,978
  • 9
  • 51
  • 86
  • What's wrong with just adding 7 days to the `start date`, saving the result, adding 7 more days, etc. until you reach the `repeat until` date? – Ken White May 29 '12 at 23:26
  • 1
    OK. :-) You didn't answer my question, though; did you read what I wrote? The solution is there. – Ken White May 29 '12 at 23:35
  • Issue is I dont know how to convert the date to timestamp so that I can add 7 days and format the result... – Stephane May 29 '12 at 23:41
  • 2
    This line `` doesn't assign the return value to anything. Change it to ``, so you're actually assigning the return value. You'll also need a `while` loop to repeat the increment until you've reached (or passed) `repeat until`. – Ken White May 29 '12 at 23:52
  • thx I've added the assignment to s_date now I'm working on the loop... – Stephane May 29 '12 at 23:57
  • @StephaneKouakou - Not that it should make any difference with dates, but which version of CF are you using - 7, 8 *=OR=* 9 ? :) – Leigh May 30 '12 at 00:34
  • I'm not sure to be honnest :) – Stephane May 30 '12 at 02:32

1 Answers1

2

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>
nosilleg
  • 2,143
  • 1
  • 22
  • 36