looping over dates

ColdFusion Add comments

did you know you can loop over dates like this;

<cfset startdate= “06/01/2004″>
<cfset enddate = “10/06/2004″>

<cfloop from=“#startdate#” to=“#enddate#” index=“i”>
<cfoutput>
#dateformat(i, “mm/dd/yyyy”)#<br />
</cfoutput>
</cfloop>


It came as news to me too! Someone had asked on cf-talk how to loop between two dates outputting the entire list (not unlike my question last week about how to loop between two dates but only display the month - as i’m using in my archives pod). Initially i’d responded to use the dateadd() function, but then i tried the above code and was amazed when it didn’t error seeing as it’s not mentioned in the docs as being valid!

2 Responses to “looping over dates”

  1. Allen Manning Says:

    This is pretty cool!

    I like CF for its 4GL style advantatges. CFLOOP is supposed to be very performant as well…

  2. Paul Roe Says:

    I am very supprised this works! However I would be remise if didn’t mention that you should probably do this:

    <cfoutput>
    <cfloop from="#startdate#" to="#enddate#" index="i">
    #dateformat(i, "mm/dd/yyyy")#<br />
    </cfloop>
    </cfoutput>

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in