0

I have this working nicely in tag format, but am trying to migrate everything into cfscript. How can I do this one? (essentially, it loops over from date1 to date2, and needs to be in 15 minute intervals.

<cfset from=now()>
<cfset to=dateadd("d", 1, from)>
<cfloop from="#from#" to="#to#" index="i" step="#CreateTimeSpan(0,0,15,0)#">
 ...stuff...
<cfloop>

It's how to specify the step bit which is getting me..

Neokoenig
  • 1,102
  • 7
  • 16
  • 2
    In the future, you might find [Peter Freitag's CFSCRIPT cheat sheet](http://www.petefreitag.com/cheatsheets/coldfusion/cfscript/) useful. – Regular Jo Nov 22 '14 at 16:20

2 Answers2

6

@Jarede's answer certainly gives you a loop which performs the same iterations with the same values as your requirement, but it's not really equivalent to the tag version. This is the closest to your example:

from    = now();
to        = dateadd("d", 1, from);
step    = CreateTimeSpan(0,0,15,0);
for (i=from; i <= to; i+=step){
    // ... stuff ...
}

If you're incrementing (or decrementing) and index value, use a for() loop, If your condition is not based around an index value, use a do or a while loop.

As I mentioned in a comment above, if you are not familiar with CFScript, you need to be. I recommend reading this, thoroughly: CFScript. It's the only complete documentation of CFScript I'm aware of. If you notice any omissions... send me a pull request.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • Thanks Adam; I'd actually been through your doc, but for some reason had thought the normal iterator of i++ would only allow a numeric increment - good to see I was wrong! The solution above solved my immediate problem, which is great, but I'm glad to see there is a direct script equivalent of what I was after. Ta! – Neokoenig Nov 22 '14 at 21:31
  • 5
    It *is* a number ;-) All three of the variables ie `from/to/step` are numbers. They are date/time objects, which are represented by numbers internally. That is why you can use them in a numeric from/to loop. – Leigh Nov 23 '14 at 01:50
2
<cfscript>
  variables.dtNow = now();
  variables.dtTmrw = dateAdd('d',1,variables.dtNow);

  do {
    variables.dtNow = dateAdd('n',15,variables.dtNow);
    writeOutput(variables.dtNow);
  } while(variables.dtNow neq variables.dtTmrw);
</cfscript>
Jarede
  • 3,310
  • 4
  • 44
  • 68