Looping Over Times In ColdFusion

Posted May 7, 2007 at 9:09 AM

Tags: ColdFusion

Looping over times in ColdFusion is the same exact thing as looping over dates in ColdFusion, but sometimes, people don't quite make the mental leap on their own. And so, I thought I would just quickly demonstrate that this was possible.

The key to looping over times is to understand that a time value is really just a fractional date value and that when looping over it, your loop increment (step value) should also be a fractional time value as well. In this case, we are going to loop from 8:00 AM to 5:00 PM using a one hour increment:

 Launch code in new window » Download code as text file »

  • <!---
  • When looping over the hours in the day, we are going to
  • use an index loop and for each iteration, we are going
  • to increase the index by a single hour (the numeric
  • timespan representing an hour fraction of the day).
  • --->
  • <cfset dtHour = CreateTimeSpan(
  • 0, <!--- Days. --->
  • 1, <!--- Hours. --->
  • 0, <!--- Minutes. --->
  • 0 <!--- Seconds. --->
  • ) />
  •  
  •  
  • <!---
  • When looping over the hours in the day, we can enter the
  • start and end time as "readable" time. ColdFusion will
  • automatically convert those time strings to their numeric
  • equivalents for use within the index loop.
  • --->
  • <cfloop
  • index="dtTime"
  • from="8:00 AM"
  • to="5:00 PM"
  • step="#dtHour#">
  •  
  • #TimeFormat( dtTime, "hh:mm TT" )#<br />
  •  
  • </cfloop>

Running the above gives us:

08:00 AM
09:00 AM
10:00 AM
11:00 AM
12:00 PM
01:00 PM
02:00 PM
03:00 PM
04:00 PM
05:00 PM

Notice that in order to get our CFLoop step value, we are building a time span object (returns a Double value) that is created using just a single hour. The mental leap to make here is that that time span value could be anything you want. It could be days, hours, minutes, or seconds.

Just remember, dates are numeric values. Because an indexed CFLoop is iterating over numeric values, ColdFusion accomplishes this by converting your date/time value for the from/to attributes to numeric values behind your back. Embrace this, do not fear it :)

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 18, 2007 at 10:05 AM // reply »
3 Comments

That was just what I needed to figure this out, thank.


Aug 27, 2007 at 7:12 PM // reply »
1 Comments

<cfloop
index="EdtTime"
from="8:00 AM"
to="8:00 PM"
step="#dtHour#">

there is an issue with 12 hour time spans, try the above code


Aug 28, 2007 at 7:31 AM // reply »
6,516 Comments

@Trent,

I just ran this:

<cfset dtHour = CreateTimeSpan( 0, 1, 0, 0 ) />

<cfloop
index="EdtTime"
from="8:00 AM"
to="8:00 PM"
step="#dtHour#">

#TimeFormat( EdtTime, "hh:mm TT" )#<br />

</cfloop>

... and it worked just fine. You have to use the TimeFormat(). Are you? If you don't, it just comes out as fractions.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »