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. 

Syntax

<cfloop 
from = "start time" 
to = "end time" 
index = "current value" 
step = "increment"> 
</cfloop>

See also

cfabortcfbreakcfcontinuecfdirectorycfexecutecfexitcfifcflocationcfrethrowcfswitchcfthrowcftrycfloop

and cfbreak in the Developing ColdFusion Applications

Attributes

Attribute

Req/Opt

Default

Description

fromDate

Required

 

The beginning of the date or time range.

toDate

Required

 

The end of the date or time range.

index

Required

 

Numeric index value. ColdFusion sets it to the numeric equivalent of the from value and increments by the numeric equivalent of the step value, until it equals the numeric equivalent of the to value.

step

Optional

 1 day

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 fromDate = Now()> 
<cfset toDate = Now() + 30> 
<cfloop from="#fromDate#" to="#toDate#" index="i" step="#CreateTimeSpan(7,0,0,0)#"> 
<cfoutput>#dateformat(i, "mm/dd/yyyy")#<br /></cfoutput> 
</cfloop>

The following example is the same as the first example, except it uses the .dateTimeFormat() member function instead of the dateFormat() function:

<cfset fromDate = Now()> 
<cfset toDate = Now() + 30> 
<cfloop from="#fromDate#" to="#toDate#" index="i" step="#CreateTimeSpan(7,0,0,0)#"> 
<cfset i = dateAdd("d", 0, i)><!--- converts number to date --->
<cfoutput>#i.dateTimeFormat("mm/dd/yyyy")#</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>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy