pf » Looping over Date Ranges
August 19, 2004
Looping over Date Ranges
On the CF-Talk mailing list today someone asked how to loop over a range of dates, eg from 6/1/04 to 8/6/04. Most people including myself responded that you can use DateAdd and cfloop to do this. But John Beynon posted the simplest solution:
<cfset startDate= "06/01/2004"> <cfset endDate = "08/06/2004"> <cfloop from="#startDate#" to="#endDate#" index="i"> <cfoutput>#dateformat(i, "mm/dd/yyyy")#<br /></cfoutput> </cfloop>
I didn't know you could use dates in the from and to attributes of cfloop. I had to try it myself, and sure enough it works. I checked the documentation and I didn't see any mention of it - learn something new everyday.
Trackback Address: 151/8CBDC244F2A1153ED121873A03A63762
Comments
On 08/19/2004 at 2:10:22 PM MDT johnb wrote:
1
it was a surprise to me too when i tried it out after first posting the dateadd() response! I thought i'd have seen an error but it seems to work!!!
On 08/19/2004 at 3:08:16 PM MDT Christopher Wigginton wrote:
2
and to expand on this....
<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>
On 08/19/2004 at 4:58:44 PM MDT Pete Freitag wrote:
3
That's pretty cool Christopher!
On 10/09/2004 at 2:53:16 PM MDT Pinal wrote:
4
buddy.. you come to my help here... this really rocks...
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
Subscribe to my RSS Feed:
RSS
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in










