DateAdd function - producing weird results

Hi all

I’ve problems using the DateAdd function.

Sourcecode:

<cfoutput>#GetLocale()#</cfoutput><br>
<cfoutput><cfset SetLocale('swiss german')></cfoutput>
<cfoutput>#GetLocale()#</cfoutput><br>

<cfset j = 5>
<cfset start_date = "10.11.2014">
<cfloop index = "LopingLouis" from = "1" to = "#j#"> 
  <cfset start_date = #LSDateFormat(DateAdd('m', +1, start_date))#>
  <cfset start_date = #LSDateFormat(start_date, "01.mm.yyyy")#>
  <cfoutput>#start_date#</cfoutput><br>
  <cfset j = j - 1>
</cfloop>

The result is:

01.11.2014
01.02.2014
01.02.2014
01.02.2014
01.02.2014

If i do it like this:

<cfoutput>#GetLocale()#</cfoutput><br>
<cfoutput><cfset SetLocale('swiss german')></cfoutput>
<cfoutput>#GetLocale()#</cfoutput><br>

<cfset j = 5>
<cfset start_date = lsparseDateTime(date="11.10.2014", format="dd.mm.yyyy")> 
<cfloop index = "LopingLouis" from = "1" to = "#j#">
  <cfset start_date = #LSDateFormat(DateAdd('m', +1, start_date))#>
  <cfset start_date = #LSDateFormat(start_date, "01.mm.yyyy")#>
  <cfoutput>#start_date#</cfoutput><br>
  <cfset j = j - 1>
</cfloop>

The result is like this:

01.02.2014
01.02.2014
01.02.2014
01.02.2014
01.02.2014

I would expect such an output:

01.11.2014
01.12.2014
01.01.2015
01.02.2015
01.03.2015

Compared to CF9:

<cfoutput>#GetLocale()#</cfoutput><br>
<cfoutput><cfset SetLocale('de_CH')></cfoutput>
<cfoutput>#GetLocale()#</cfoutput><br>

<cfset j = 5>
<cfset start_date = "10. Okt 2014">
<cfloop index = "LopingLouis" from = "1" to = "#j#"> 
  <cfset start_date = #LSDateFormat(DateAdd('m', +1, start_date))#>
  <cfset start_date = #LSDateFormat(start_date, "01. mmm yyyy")#>
  <cfoutput>#start_date#</cfoutput><br>
  <cfset j = j - 1>
</cfloop>

Produces this output:

01. Nov 2014
01. Dez 2014
01. Jan 2015
01. Feb 2015
01. Mrz 2015

I really don’t understand that all. Any idea what i’m doing wrong?

Win2k16
JAVA_VERSION=“11.0.3”
Tomcat 9.0.14
Lucee 5.3.8.206

Hi all

With the help of @Terry_Whitney from this post i was able to make it work.

<cfset j = 95>
<cfset start_date = lsparseDateTime(date="11.10.2014", format="dd.mm.yyyy")> 

<cfloop index = "LopingLouis" from = "1" to = "#j#">
  <cfset start_date = DateAdd("m", +1, "#start_date#")>
  <cfoutput>#LSDateFormat(#start_date#, "dd.mm.yyyy")#</cfoutput><br>
  <cfset j = j - 1>
</cfloop>

Output

11.02.2014
11.03.2014
11.04.2014
11.05.2014
11.06.2014
11.07.2014
11.08.2014
11.09.2014
11.10.2014
11.11.2014
11.12.2014
11.01.2015
11.02.2015
(...)

Thanks Terry!

4 Likes

Just to note… The issue occurred because of the start_date variable switchng from type date object to type string. In the loop you are setting your start-date date object to a string by using lsdateformat() (lsdateformat () converts a date object to a string). In the next loop step your dateadd function expects a date object, but it gets that string. The CF engine will try converting it, but that can always have unpredictable results, as happened here.

Also, I think you can remove the <cfset j = j - 1>, because I think it’s not needed in that example.

1 Like

@andreas Thanks for your clarification.

Also, I think you can remove the <cfset j = j - 1>, because I think it’s not needed in that example.
You’re right, we don’t need it for this sample. It’s a piece of code that is relevant for other parts in the whole code not seen here - i did not remove it here. My bad.

Optionally…

#LSDateFormat(DateAdd('m',LopingLouis,Start_Date), "dd.mm.yyyy")#