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. 

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>

Output

11/26/2018 12/03/2018 12/10/2018 12/17/2018 12/24/2018

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>

Output

11/26/2018 12/03/2018 12/10/2018 12/17/2018 12/24/2018

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>

Output

12:00 AM
12:30 AM
01:00 AM
01:30 AM
..........

 Adobe

Saņemiet palīdzību ātrāk un vienkāršāk

Jauns lietotājs?

Adobe MAX 2024

Adobe MAX
Radošuma konference

14.–16. oktobris Maiami pludmalē un tiešsaistē

Adobe MAX

Radošuma konference

14.–16. oktobris Maiami pludmalē un tiešsaistē

Adobe MAX 2024

Adobe MAX
Radošuma konference

14.–16. oktobris Maiami pludmalē un tiešsaistē

Adobe MAX

Radošuma konference

14.–16. oktobris Maiami pludmalē un tiešsaistē