Looping Over Times In ColdFusion

Posted May 7, 2007 at 9:09 AM by Ben Nadel

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:

  • <!---
  • 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 :)




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 »
10,638 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.


Jun 1, 2011 at 9:20 AM // reply »
4 Comments

Hey guys, thanks for the examples. I'm having issues displaying times from 8pm to 4am (see code below). If you copy+paste that, nothing displays. Any help is appreciated.

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

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

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

</cfloop>


Jun 1, 2011 at 10:32 AM // reply »
10,638 Comments

@Alan,

You can't do that because there is no "date" involved. As such, it sees those times as already having passed each other (4AM is less than 8PM of the *same* day).

I suspect that in order to get that to work, you would need to add an actual date to the time:

  • <cfloop
  • index="edtTime"
  • from="#createDateTime( 2011, 1, 1, 20, 0, 0 )#"
  • to="#createDateTime( 2011, 1, 2, 4, 0, 0 )#"
  • step="#dtHour#">
  •  
  • ... your code ...
  •  
  • </cfloop>

This way, you're not just using hours, you actually going from the hours in ONE day to the hours in the NEXT day.

See if that works. The date part is really irrelevant as long as the day in the TO is one more than the day in the FROM.


Jun 1, 2011 at 2:35 PM // reply »
4 Comments

Thanks Ben - your example reminded me to use military time which pretty much solves the issue.


Jun 1, 2011 at 3:44 PM // reply »
10,638 Comments

@Alan,

Glad you got it worked out; but, how did military time solve the problem? Wouldn't 20:00 till be greater than 04:00? It seems you'd still have a loop that wouldn't start?


Jun 1, 2011 at 3:48 PM // reply »
4 Comments

Yes, but you got me thinking about how to "add time" to days, so to speak. Here is what I ended up doing:

  • <cfset theDate = #form.startDate#>
  • <cfset theHour = #form.hour#>
  •  
  • <cfset dtHour = CreateTimeSpan( 0, 1, 0, 0 ) />
  •  
  • <cfset shiftLength=8>
  • <cfset shiftDateTime=theDate & " " & theHour>
  • <cfset startTime=ParseDateTime(shiftDateTime)>
  • <cfset endTime=DateAdd('h',shiftLength,startTime)>
  •  
  • <select name="earlyOutHour">
  •  
  • <cfloop index="edtTime" from="#StartTime#" to="#EndTime#" step="#dtHour#">
  • <option value="#TimeFormat(EdtTime, "hh:mm TT" )#">#TimeFormat(EdtTime, "hh:mm TT" )#</option>
  • </cfloop>
  •  
  • </select>


Jun 1, 2011 at 3:52 PM // reply »
10,638 Comments

@Alan,

Ahh, cool! I see where you went with that. Well, glad we could get this done!


Jun 1, 2011 at 3:57 PM // reply »
4 Comments

@Ben,

Yeah man, so thanks again. Shout out to my buddy Mark who also helped out!


Jun 1, 2011 at 3:58 PM // reply »
10,638 Comments

@Alan,

Ha ha, heck yeah - high-fives all around :D


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 3, 2012 at 10:49 PM
How I Got Node.js Running On A Linux Micro Instance Using Amazon EC2
Wow this was really helpful! Only thing I would add is you need to update your .bash_profile after you edit the secure_path. This is what I did: $ . ~/.bash_profile Otherwise, NPM won't be found. ... read »
Feb 3, 2012 at 10:14 PM
Pushing Base64-Encoded Images Over HTML5 WebSockets With Pusher And ColdFusion
@Ben, Just wanted to let you know that pusher are soon to start limiting sizes on messages. This was the detail that came through in the Feb dispatch: "However, we will soon be limiting the s ... read »
Feb 3, 2012 at 5:05 PM
Regular Expressions Make CSV Parsing In ColdFusion So Much Easier (And Faster)
I tried using your RegEx in my C# program, but it was matching an extra empty-string at the end and so I would end up with an extra field that doesn't exist, so I changed it to this: (^|,)("(?: ... read »
Feb 3, 2012 at 3:47 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
Josh Cyr posted this on Twitter just a little bit ago. Thought it was appropriate. http://stackoverflow.com/questions/1619152/how-to-create-rest-urls-without-verbs/1619677#1619677 ... read »
Feb 3, 2012 at 2:28 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
@Michael, You definitely make a good point (and extra points for quoting movies - I love movies). When you use a return() statement to define the object's public API, it does provide a consistent a ... read »
Feb 3, 2012 at 2:04 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
To quote Jurassic Park: "Just because you can doesn't mean you should". I completely, utterly disagree with the thought that this is more readable. Consider the current module pattern: if ... read »
Feb 3, 2012 at 1:10 PM
REST API Design Rulebook By Mark Masse
@Jordan, Yeah, WRML was created by Mark Masse (author of the book). I also found it to be a bit convoluted. I suppose it is intended to allow the Client to be able to programmaticaly respond to cha ... read »
Feb 3, 2012 at 1:08 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
@Jason, To be honest, I don't have good answers for that kinds of stuff. And, to the point, that is specifically why I *really* liked the REST API Design Rulebook by Mark Masse - he just cuts throu ... read »