Notice that we are looping from 9AM to 10AM and are using a Step value of 5 minutes as determined by ColdFusion's CreateTimeSpan() method. When we run the above code (and this was the same on Ray's blog), we get the following output:

09:0009:0509:1009:1509:2009:2509:3009:3509:4009:4509:5009:5510:00

10:0010:0510:1010:1510:2010:2510:3010:3510:4010:4510:5010:55

Notice that the first loop runs 13 times, ending on the following hour, while the second loop runs only 12 times and ends on the 55 of the current hour.

So what's going on here? As Ray pointed out in his post, the value of the loop index becomes a floating point number - a number representation of the date. But, when I look at the above loop, you know what really sticks out at me in a huge way? The use of time values like "9 AM" and "10 AM". Something about this just seems really funky. I can't tell you why - it's just a gut feeling.

Acting on the gut feeling, I decided to make my From and To attributes a bit more explicit as far as time. Since we don't really care about the date, just the time, I'm going to define the From and To attributes in the same way that I'm defining my CFLoop step value - using CreateTimeSpan():

<!--- Create 5 minute interval step. --->

<cfset dtStep = CreateTimeSpan( 0, 0, 5, 0 ) />

<!--- Go from 9 AM to 10 AM every 5 minutes. --->

<cfloop

index="dtNow"

from="#CreateTimeSpan( 0, 9, 0, 0 )#"

to="#CreateTimeSpan( 0, 10, 0, 0 )#"

step="#dtStep#">

#TimeFormat( dtNow, "HH:mm" )#<br />

</cfloop>

<br />

<!--- Go from 10 AM to 11 AM every 5 minutes. --->

<cfloop

index="dtNow"

from="#CreateTimeSpan( 0, 10, 0, 0 )#"

to="#CreateTimeSpan( 0, 11, 0, 0 )#"

step="#dtStep#">

#TimeFormat( dtNow, "HH:mm" )#<br />

</cfloop>

This time, we aren't letting ColdFusion interpret "9AM" or "10AM" as a valid date/time stamp - we are telling it explicitly how to handle our numeric date representations. And, when we run this code, we get the following output:

09:0009:0509:1009:1509:2009:2509:3009:3509:4009:4509:5009:5510:00

10:0010:0510:1010:1510:2010:2510:3010:3510:4010:4510:5010:5511:00

Sweeet! This time, both CFLoops iterate 13 times and end on the following hour. This is the kind of behavior we want to see.

Having done this, I then got curious as to whether it was a numeric date representation issue, or if it was a string-to-date/time conversion issue. To test this, I ran another experiment, this time defining the From and To attributes using CreateTime(). CreateTime(), just like CreateDate(), does not create a numeric date, but rather a standard ColdFusion date/time stamp based off of the ColdFusion "zero date":

CreateTime( 9, 0, 0 ) == {ts '1899-12-30 09:00:00'}

As you can see, CreateTime() creates a valid date/time stamp (just like Now() or ParseDateTime()). I put this into the CFLoop attributes:

<!--- Create 5 minute interval step. --->

<cfset dtStep = CreateTimeSpan( 0, 0, 5, 0 ) />

<!--- Go from 9 AM to 10 AM every 5 minutes. --->

<cfloop

index="dtNow"

from="#CreateTime( 9, 0, 0 )#"

to="#CreateTime( 10, 0, 0 )#"

step="#dtStep#">

#TimeFormat( dtNow, "HH:mm" )#<br />

</cfloop>

<br />

<!--- Go from 10 AM to 11 AM every 5 minutes. --->

<cfloop

index="dtNow"

from="#CreateTime( 10, 0, 0 )#"

to="#CreateTime( 11, 0, 0 )#"

step="#dtStep#">

#TimeFormat( dtNow, "HH:mm" )#<br />

</cfloop>

When we run this code, we get the following output:

09:0009:0509:1009:1509:2009:2509:3009:3509:4009:4509:5009:5510:00

10:0010:0510:1010:1510:2010:2510:3010:3510:4010:4510:5010:55

Interesting! This methodology fails in the same was as our first methodology (using values like "9AM").

So, what does this mean? We can conclude that the issue is not in how ColdFusion converts a date/time string into a date/time stamp; rather, the issue is in how ColdFusion converts a date/time stamp into a numeric date representation. To get around this, we have two choices: either, we modify the CFLoop to use DateAdd() as Ray pointed out in his blog post; or, if we want to use CreateTimeSpan() for our increment, we must make sure to define all of our limits using CreateTimeSpan().

Reader Comments

Wow, some good timely info to know! I just finished working on an application that is looping over times of the day like this. I'll have to check my application to see if this error is possible when times are defined as "10:00 AM" and "11:00 AM" which is how I'm using the cfloop rather than "10 AM" and "11 AM".

As a side note, you need almost none of those "#" signs. You only need a "#" when you are evaluating a ColdFusion variable inside a string. All other areas of use are unnecessary.

That said, I cannot run this as I don't have the "utility" component. But, from what I've seen, you are changing the names variable. Do you mean to do that? At first you have "names" as an array. Then, later on you have:

Ben, thanks for this, I know it is pretty old but I found it very useful today! I also needed a way to create a list of days. I don't know if this has been mentioned before but I found this to work for days:

I am the co-founder and lead engineer at InVision App, Inc — the world's leading prototyping,
collaboration & workflow platform. I also rock out in JavaScript and ColdFusion 24x7 and I dream about
promise resolving asynchronously.