Looping for months not displaying the last month

I'm trying to fixate on motnhs sort of.

<cfloop index="i" from="05-2012" to="12-2012" step="#createTimeSpan(31, 0, 0, 0)#">
        #LSDateFormat(i, "MMM")#  
</cfloop>

but it only displays months until November 2012. To show December 2012, I have to put

#LSDateFormat(i, "MMM")#  

again after the cycle. any suggestion?

+3
source share
1 answer

I guess this is because "12-2012" is "01-12-2012" and you are using a 31-day step (which does not coincide with one month). Thus, the last iteration is looking for 04-12-2012, while your "before" is 12-12-2012. You can easily see this problem as follows:

<cfloop index="i" from="05-2012" to="12-2012" step="#createTimeSpan(31, 0, 0, 0)#">
    #LSDateFormat(i)#<br/>
</cfloop>

A simple solution would be the following:

<cfloop index="i" from="#CreateDate(2012, 5, 1)#" to="#CreateDate(2012, 12, 31)#" step="#CreateTimeSpan(31, 0, 0, 0)#">
    #LSDateFormat(i)#<br/>
</cfloop>

Plus it looks a little more readable to me.

, . span 1 , , - :

<cfset i = CreateDate(2012, 5, 1) />
<cfset stop = CreateDate(2012, 12, 31) />
<cfloop condition="i LTE stop">
    #LSDateFormat(i)#<br/>
    <cfset i = DateAdd("m",1,i)>
</cfloop>

, .

+9

All Articles