ColdFusion Muse

Interesting Loop for Dates and Times

Mark Kruger October 17, 2008 5:59 PM Coldfusion Tips and Techniques Comments (11)

Now my readers know that I've seen enough Coldfusion code in my day that I can quote some livedoc articles verbatim (including the notes and comments). It takes a great deal to surprise me with something new. But here's a snippet I found the other day that made me sit up and take notice. The fact that it uses CFLOOP (one of the most ho-hum of all the CF tags) is even more surprising. This is a loop that iterates over time increments. You can use it to create incremental time objects that are n number of minutes or seconds apart. It doesn't look like you might expect either. Here is an example that loops from 8 to 5 in 45 minute intervals:

<cfloop index="tm" from="8:00 AM" to="5:00 PM" step="#createTimespan(0,0,45,0)#">    
    <cfoutput> <li>#TimeFormat( tm, "h:mm TT" )#</li></cfoutput>

The output:

  • 8:00 AM
  • 8:45 AM
  • 9:30 AM
  • 10:15 AM
  • 11:00 AM
  • 11:45 AM
  • 12:30 PM
  • 1:15 PM
  • 2:00 PM
  • 2:45 PM
  • 3:30 PM
  • 4:15 PM
  • 5:00 PM

Now you have to admit that's pretty neat - and pretty usefule too. You could easily see using this in a calendar application where you are scheduling appointments. It would let you add a drop down of available times. Nifty!

Before you get too excited and knock over your sippy cup you should know that this very useful bit of code is extremely expensive as loops go. It's roughly 50 times longer than the equivalent from="1" to="12" type loop. This is not a problem if you are building a single drop down or outputting a single bulleted list of times like the one above. But if you are going to run this loop over and over again you should know that it slows down drastically the more times you run it in a single request. I had a page where I was doing a "dateDiff()" call and examining a time element against the loop index like so:

<cfloop index="dtTime" from="1:00 AM" to="12:00 PM" step="#createTimespan(0,0,5,0)#">    
    <cfif datediff('n',externalTime,tm) GT 0>
        #TimeFormat( dtTime, "h:mm TT" )#
And on the third or fourth iteration the loop cost was as much as 3 to 5 seconds. Of course it was a busy server, but still, 3 to 5 seconds? Anyway, I'm sure that muse readers will now regale me with tales about how they have used this looping method since version 3.5. Post away me hearties.

  • Share:


  • Ben Nadel's Gravatar
    Posted By
    Ben Nadel | 9/17/08 4:08 PM
    Mark, I love date/time looping. Sometimes, it feels like the best thing since sliced bread.

    I am very curious as to why you were seeing such performance issues. I believe, behind the scenes, ColdFusion is just converting the time loop into a basic numeric loop (after all, CreateTimeSpan() creates a numeric representation of date/times).
  • Glyn's Gravatar
    Posted By
    Glyn | 9/17/08 4:11 PM
    I have never seen this, thanks. i wonder if it would work with 12 hour clock values also.
  • jc's Gravatar
    Posted By
    jc | 9/17/08 4:12 PM
    I could see doing it once on a throwaway page to save writing a bunch of HTML, just copy and paste the output or something... I've done that sort of thing lots of times.

    The last time I remember doing something like this was calculating out my personal paydates for the next 5 years and flagging which months had three paychecks.
  • mark kruger's Gravatar
    Posted By
    mark kruger | 9/17/08 4:17 PM

    The problem was pretty specific. It had to do with date formatting and comparison - going from string to date to string etc... Inside the loop it was simply too expensive and the problem was compounded by the type of loop I think. Rather than try and figure out the who what and why - I just wrote one of my rules of thumb... don't use this loop too often in a single page :)

  • Ben Nadel's Gravatar
    Posted By
    Ben Nadel | 9/17/08 4:20 PM

    Fair enough :) Use only when it helps.
  • Chris Tierney's Gravatar
    Posted By
    Chris Tierney | 9/17/08 4:30 PM
    Gee, the times I could have used this!
  • Tawanna's Gravatar
    Posted By
    Tawanna | 9/29/08 11:22 AM
    Wow! That is pretty neat. I've never seen that before either.. Definitely nice for a scheduling type of application.
  • duncan's Gravatar
    Posted By
    duncan | 10/22/08 2:38 PM
    Coincidentally, today I saw a colleague using dates for the from/to values, which I'd never seen before either.
  • John Whish's Gravatar
    Posted By
    John Whish | 10/23/08 4:44 AM
    I knew you could loop through dates but it never occurred to me that it would work with time as well! Thanks for posting the tip!
  • victor's Gravatar
    Posted By
    victor | 3/14/12 3:48 PM
    Just what i was looking for. I need to overide it a bit though to work with datetime objects, and increment dates or times and accept passes in increment variables. THanks aLot!;-)
  • Alli's Gravatar
    Posted By
    Alli | 11/22/13 10:51 AM
    New to all of this. Please show how you can turn the original post into a pull down.