cfloop: looping over a date or time range

Description

Loops over the date or time range specified by the from and to attributes. By default, the step is 1 day, but you can change the step by creating a timespan. The cfloop tag loops over tags that cannot be used within a cfoutput tag.

Syntax

<cfloop 
   from = "start_time"
   to = "end_time"
   index = "current_value"
   step = "increment">
</cfloop>

See also

cfabort, cfbreak, cfdirectory, cfexecute, cfexit, cfif, cflocation, cfrethrow, cfswitch, cfthrow, cftry; cfloop and cfbreak in Elements of CFML in ColdFusion MX Developer's Guide

Attributes

Attribute Req/Opt Default Description

from

Required

 

The beginning of the date or time range.

to

Required

 

The end of the date or time range.

index

Required

1 day

Index value. ColdFusion sets it to the from value and increments by the step value, until it equals the to value.

step

Optional

 

Step, expressed as a timespan, by which the index increments.

Example

The following example loops from today's date to today's date plus 30 days, stepping by 7 days at a time and displaying the date:

<cfset startDate = Now()> 
<cfset endDate = Now() + 30>
<cfloop from="#startDate#" to="#endDate#" index="i" step="#CreateTimeSpan(7,0,0,0)#">
  <cfoutput>#dateformat(i, "mm/dd/yyyy")#<br /></cfoutput>
</cfloop>

The following example displays the time in 30-minute increments, starting from midnight and ending 23 hours, 59 minutes, and 59 seconds later:

<cfset startTime = CreateTime(0,0,0)> 
<cfset endTime = CreateTime(23,59,59)> 
<cfloop from="#startTime#" to="#endTime#" index="i" step="#CreateTimeSpan(0,0,30,0)#"> 
 <cfoutput>#TimeFormat(i, "hh:mm tt")#<br /></cfoutput>
</cfloop>

View comments in LiveDocs