tracker issue : CF-3701665

select a category, or use search below
(searches all categories and all time range)

Unexpected result when diffing dates

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): danny armstrong / danny armstrong (danny armstrong)

Created: 01/30/2014

Components: Language

Versions: 9.0.1

Failure Type:

Found In Build/Fixed In Build: 9.0.1 /

Priority/Frequency: Major / All users will encounter

Locale/System: English / Linux All

Vote Count: 2

Listed in the version 2016.0.0.297996 Issues Fixed doc
Verification notes: verified_fixed on August 25, 2019 using build 2016.0.01.298513
Problem Description:

Sometimes when two dates are compared the result is not what is expected. Here is example code that shows for most days of the year the difference is 32 months. For four days a year the result is 31.

Steps to Reproduce:

Run this code:
from = "01/01/2000"
to = "01/01/2018"
index = "date"
step = "1">
  <cfset datePlus32 = dateAdd('m', 32, date)/>
  <cfset monsDiff = dateDiff("m", date, datePlus32)/>
  <cfoutput>#datePlus32#, #monsDiff#, #IIF(monsDiff EQ 31, DE("<a style='color: red;'>!!</a>"), DE(""))#<br/></cfoutput>

Actual Result:
Most days report 32 month difference but 4 times a year the result is 31.

Expected Result:
I expect 32 to be the result every time. Instead 4 times a year the result is 31.

Any Workarounds:
Use database to do date math :(

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3701665

External Customer Info:
External Company:  
External Customer Name: detarmstrong
External Customer Email:  
External Test Config: My Hardware and Environment details:

coldfusion 9.0.1, update level /opt/coldfusion9/lib/updates/hf901-00010.jar  

ubuntu 12.04 64bit

apache 2.2.22

Also confirmed on



dateDiff needs to work consistently in all circumstances
Vote by External U.
13553 | January 31, 2014 12:37:03 PM GMT
I can't reliably add and diff dates due to this bug.
Vote by External U.
13554 | February 03, 2014 08:25:53 PM GMT
Folks, about the "4 times a year" issue, I think the explanation is in the fact that there are 4 months which follow months with only 30 days (May, July, October, and December.) If you add a "month" to the last day of one of those 30-day months, what should CF add? 30 days? 31? should it pick the day that's the same number of days into the next month? And what if there are fewer, as is true 5 times a year (including also February)? Then, similarly., what should a diff of 1 month be, when you're on the 31st of one of those 4 months where the previous month has only 30 days? Should it report 0? or 1? I really think that's what's happening here, but it may have been a bit harder to see with the focus on 32 months. If you review the output of the dateadd32, you'll see oddities in the dates that have fewer than 31 days...which begs the same question about what CF should do when "a month" is added to dates when the next month has more days: should it add 30? 31? should it be the same relative number of days into the next month? and what if there are fewer?). And if you change it instead to do a 1 month diff, and output the diff for 1 month, you'll see clearly the 4 months that report a 0, for the reason I discuss above. Hope that's helpful.Not sure of any good workaround, though. It's about what adding or subtracting a month should always mean. I just don't think it's obvious. Others can correct me.
Comment by External U.
13549 | February 04, 2014 12:56:59 AM GMT
This appears to have already been reported in bug id: CF-3041034 I think the issue would be less surprising as a developer if dateDiff and dateAdd behaved consistently. As it stands one cannot round trip between adding and then diffing while expecting a consistent result. Other date libraries like moment.js for javascript behave as expected. See this jsfiddle that demonstrates similar code as the bug report sample:
Comment by External U.
13550 | March 02, 2014 08:42:10 PM GMT
In DateDiff, when finding month differences, we were having a special check for cases like, Feb 28, 1999 to March 30, 1999 counts as 0 months To 28th of Feb, when we add 1 month, it becomes 28th Mar. Now when we do differ, 28th is the last day of the Feb, but 28th is not the last day of march, so we were not considering it as one month. Checked in the joda-time library and moment.js. DateTime date = new DateTime().withDate(2001, 02, 28); System.out.println(date); DateTime newDate = new DateTime().withDate(2001, 03, 28); // DateTime newDate = date.plusMonths(1); System.out.println(newDate); System.out.println(Months.monthsBetween(date, newDate).getMonths()); The difference is one. Whether it is at the end of the month or in middle of the month, they are not checking. Similarly in the moment.js, They are specifically saying, "Feb 28 to Mar 28 should be exactly 1 month.". So removing the check here.
Comment by Paul N.
13551 | November 19, 2014 04:08:44 AM GMT
Fix verified in ColdFusion build #294424 (Comment added from ex-user id:duttswam)
Comment by Adobe D.
13552 | June 22, 2015 11:46:14 PM GMT
Hi Adobe, I've verified this is fixed in CF2016 Update 1 (build 2016.0.01.298513). Thanks!, -Aaron
Comment by Aaron N.
31210 | August 25, 2019 11:10:22 PM GMT