A reader sent in an interesting issue. He was using createTimeSpan to list out 5 minute intervals between two hours. Here is what he used:

<cfset dtHour = CreateTimeSpan( 0, <!--- Days. ---> 0, <!--- Hours. ---> 5, <!--- Minutes. ---> 0 <!--- Seconds. ---> ) />

<cfset thecount = 0> <cfloop from="9:00 AM" to="10:00 AM" step="#dtHour#" index="i"> <cfset thecount+=1> <cfoutput>#timeformat(i,"short")#<br /></cfoutput> </cfloop>

<cfoutput> #thecount# </cfoutput>

In the code above he creates a 5 minute time span and then loops from 9 AM to 10 AM. I rarely use createTimeSpan, and when I have used it, I only used it with query caching. This worked... until he added another loop:

<cfset thecount2 = 0> <cfloop from="10:00 AM" to="11:00 AM" step="#dtHour#" index="i"> <cfset thecount2+=1> <cfoutput>#timeformat(i,"short")# #i#<br /></cfoutput> </cfloop> <cfoutput> #thecount2# </cfoutput>

This should show the same results, just a different hour, right? Check out the results:

What the heck? (Actually when I ran this I said something a bit strong.) There are 13 counts in the first loop and 12 in the second. Also note the second stops at 10:55, not 11.

I was a bit lost at first, but then I remembered something. The interval value is actually a number. On a whim I modified the code to output the interval and the index within the loop:

<cfset dtHour = CreateTimeSpan( 0, <!--- Days. ---> 0, <!--- Hours. ---> 5, <!--- Minutes. ---> 0 <!--- Seconds. ---> ) /> <cfoutput>#dtHour#<p></cfoutput> <cfset thecount = 0> <cfloop from="9:00 AM" to="10:00 AM" step="#dtHour#" index="i"> <cfset thecount+=1> <cfoutput>#timeformat(i,"short")# #i#<br /></cfoutput> </cfloop>

<cfoutput> #thecount# </cfoutput> <br /><br /> <cfset thecount2 = 0> <cfloop from="10:00 AM" to="11:00 AM" step="#dtHour#" index="i"> <cfset thecount2+=1> <cfoutput>#timeformat(i,"short")# #i#<br /></cfoutput> </cfloop> <cfoutput> #thecount2# </cfoutput>

Check out the result:

Ah, floating point numbers. I think you can see here where the issue is coming up - rounding errors. Notice how even the values for 10AM (at the end of the first loop and the first entry of the second loop) don't match.

Nice, so now we know why, how can we rewrite this? Here is a slightly modified version using a conditional loop:

<!--- number of minutes ---> <cfset step = 5>

<cfset theTime = "9:00 AM"> <cfset toTime = "10:00 AM">

<cfloop condition="dateCompare(theTime,toTime) lt 1"> <cfoutput>thetime=#timeFormat(thetime)#<br></cfoutput> <cfset theTime = dateAdd("n", step, theTime)> </cfloop>

<p/>

<cfset theTime = "10:00 AM"> <cfset toTime = "11:00 AM">

<cfloop condition="dateCompare(theTime,toTime) lt 1"> <cfoutput>thetime=#timeFormat(thetime)#<br></cfoutput> <cfset theTime = dateAdd("n", step, theTime)> </cfloop>

This uses a simple numeric value for the number and passes it to the dateAdd function. What's kind of cool about this code is that you could also do non-even steps as well. (Sorry, not even as an even/odd, but a step value that won't fit evenly into the interval.)