I am trying to loop over motnhs like.
<cfloop index="i" from="05-2012" to="12-2012" step="#createTimeSpan(31, 0, 0, 0)#">
#LSDateFormat(i, "MMM")#
</cfloop>
but it displays months only till Nov 2012. in order to display the Dec 2012 I have to put
#LSDateFormat(i, "MMM")#
again after the loop. any suggestion?
I guess this is because "12-2012" is "01-12-2012" and you are using 31 days step (which is not the same as one month). So last iteration is looking for 04-12-2012 while your "to" is 01-12-2012. You can easily see this problem like this:
<cfloop index="i" from="05-2012" to="12-2012" step="#createTimeSpan(31, 0, 0, 0)#">
#LSDateFormat(i)#<br/>
</cfloop>
Simple solution would be this:
<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 bit more readable for me.
Please note that in case of longer period problem may appear again. Most reliable solution would be to make the span equal to 1 month, say use something like this:
<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>
See, it is always 1st day of the month.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With