Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

issue with datediff for months

New Here ,
Feb 18, 2009 Feb 18, 2009
In Coldfusion MX7:

#datediff('m', '6/29/2009', '7/29/2009')# (evaluates to 1, which is good)
#datediff('m', '6/30/2009', '7/31/2009')# (evaluates to 1, which is good)
#datediff('m', '6/30/2009', '7/30/2009')# (evaluates to 0, which is a little confusing)

This results in the following behavior:

#datediff('m', '6/29/2009', dateadd('m',1,'6/29/2009') )# (evaluates to 1, which is good)
#datediff('m', '6/30/2009', dateadd('m',1,'6/30/2009') )# (evaluates to 0, which is not good)
404
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 18, 2009 Feb 18, 2009
That is odd. Seems to give an unexpected answer when the first date is the last day of the month.

I tried various combinations of this:
Date3 = createdate(2009,2,27);
Date4 = createdate(2009,3,27);
writeoutput("#datediff("m",date3,date4)#");

What do the cf8 users see?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 18, 2009 Feb 18, 2009
JSBCHS wrote:
> In Coldfusion MX7:
>
> #datediff('m', '6/29/2009', '7/29/2009')# (evaluates to 1, which is good)
> #datediff('m', '6/30/2009', '7/31/2009')# (evaluates to 1, which is good)
> #datediff('m', '6/30/2009', '7/30/2009')# (evaluates to 0, which is a little
> confusing)
>
> This results in the following behavior:
>
> #datediff('m', '6/29/2009', dateadd('m',1,'6/29/2009') )# (evaluates to 1,
> which is good)
> #datediff('m', '6/30/2009', dateadd('m',1,'6/30/2009') )# (evaluates to 0,
> which is not good)
>

This is a known bit of ColdFusion date weirdness. When using the
datediff() function the difference is rounded *down* from the fractional
difference potentially down to the second. So since there are not a
full 31 days between your dates of 6/30/2009 and 7/30/2009 that there
needs to be to be a full calendar month of July, datediff() returns 0.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 18, 2009 Feb 18, 2009
LATEST
same same on cf8, too.
if first month of the 2 has 31 days the diff is 1, but if it is a month
that has 30 days the diff is 0:

<cfset dd = createdate(2009, 5, 31)>
<cfset dd2 = dateadd('m', 1, dd)>
<cfoutput>
#dd# - #dd2#<br>
datediff('m', dd, dateadd('m',1,dd) ) : #datediff('m', dd,
dateadd('m',1,dd) )#<!--- returns 1 --->
</cfoutput>


<cfset dd = createdate(2009, 6, 30)>
<cfset dd2 = dateadd('m', 1, dd)>
<cfoutput>
#dd# - #dd2#<br>
datediff('m', dd, dateadd('m',1,dd) ) : #datediff('m', dd,
dateadd('m',1,dd) )#<!--- returns 0 --->
</cfoutput>


not sure if it os odd or not though...

from datediff() docs:

"The DateDiff function determines the number of *complete datepart
units* between the two dates; for example, if the
datepart parameter is "m" and the dates differ by 55 days, the function
returns 1."

so, maybe, CF (and Java, too, i presume?) sees it as no FULL month is
encompassed when the first date has 30 days and the second has 31 days?

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources