View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > DateDiff PreviousNext

DateDiff

Determines the integer number of units by which date1 is less than date2.

A number of units, of type datepart.

Date and time functions

DateDiff("datepart", "date1", "date2")

DateAdd, DatePart, CreateTimeSpan

ColdFusion MX:

Parameter

Description

datepart

String specifying the units in which to count; for example yyyy requests a date difference in whole years.

  • yyyy: Years
  • q: Quarters
  • m: Months
  • y: Days of year (same as d)
  • d: Days
  • w: Weekdays (same as ww)
  • ww: Weeks
  • h: Hours
  • n: Minutes
  • s: Seconds

date1

Date/time object, in the range 100 AD-9999 AD.

date2

Date/time object, in the range 100 AD-9999 AD.

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.

Enclose string constant dates in quotation marks. If the text contains only numbers (such 1932), and is not surrounded by quotation marks, ColdFusion interprets it as a date/time object, resulting in an incorrect value.

<cfif IsDefined("form.value")>
   <cfset value = form.value>
</cfif>
<cfif IsDefined("form.type")>
   <cfset type = form.type>
</cfif>

<cfif IsDefined("form.date1") and IsDefined("form.date2")>

   <cfif IsDate(form.date1) and IsDate(form.date2)>
   
      <p>This example uses DateDiff to determine the difference
      in    
      <cfswitch expression = "#form.type#">
         <cfcase value="yyyy">years</cfcase>
         <cfcase value="q">quarters</cfcase>
         <cfcase value="m">months</cfcase>
         <cfcase value="y">days</cfcase>   
         <cfcase value="d">days</cfcase>   
         <cfcase value="w">weekdays</cfcase>   
         <cfcase value="ww">weeks</cfcase>   
         <cfcase value="h">hours</cfcase>   
         <cfcase value="n">minutes</cfcase>   
         <cfcase value="s">seconds</cfcase>      
         <cfdefaultcase>years</cfdefaultcase>
      </cfswitch>
          dateparts between date1 and date2.
      
      <cfif DateCompare("#form.date1#","#form.date2#") is not 0>
      <p>The difference is <cfoutput>#Abs(DateDiff(type, form.date2, form.date1))#</cfoutput>
      <cfswitch expression = "#form.type#">
         <cfcase value="yyyy">years</cfcase>
         <cfcase value="q">quarters</cfcase>
         <cfcase value="m">months</cfcase>
         <cfcase value="y">days</cfcase>   
         <cfcase value="d">days</cfcase>   
         <cfcase value="w">weekdays</cfcase>   
         <cfcase value="ww">weeks</cfcase>   
         <cfcase value="h">hours</cfcase>   
         <cfcase value="n">minutes</cfcase>   
         <cfcase value="s">seconds</cfcase>      
         <cfdefaultcase>years</cfdefaultcase>
      </cfswitch>.
      <cfelse>
      <p>The two dates are equal!  Try changing one of the values ...
      </cfif>
   
   <cfelse>
   <p>Please enter two valid date/time values, formatted like this:
   <cfoutput>#DateFormat(Now())#</cfoutput>   
   </cfif>   

</cfif>
<form action="index.cfm" method="post">

<pre>
Date 1
<input type="Text" name="date1" value="<CFOUTPUT>#DateFormat(Now())#</CFOUTPUT>">
Date 2
<input type="Text" name="date2" value="<CFOUTPUT>#DateFormat(Now())#</CFOUTPUT>">
What kind of unit to show difference?
   <select name="type">
      <option value="yyyy" selected>years
      <option value="q">quarters
      <option value="m">months
      <option value="y">days of year
      <option value="d">days
      <option value="w">weekdays
      <option value="ww">weeks
      <option value="h">hours
      <option value="n">minutes
      <option value="s">seconds
   </select>
</pre>

<input type="Submit" name=""><input type="RESET">
</form>

.

Contents > CFML Reference > ColdFusion Functions > DateDiff PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


CFGumby said on Sep 11, 2003 at 9:56 AM :
Datediff seems to calculate the
wrong number of days if the
dates span 4/07/2003 or
4/5/2004 or 4/4/2005 and
probably more. (In 6.1 only.)

<cfset DateSeed = "01/01/2003">
<table>
<tr>
<td>Date1</td>
<td>Date2</td>
<td>X</td>
<td>Days Diff</td>
</tr>
<cfloop from="0" to="1000" index="y">
<cfset Date1 = DateAdd("d", y, DateSeed)>
<cfloop from="0" to="200" index="x">
<cfset Date2 = DateAdd("d", x, Date1)>
<cfset DaysDiff = datediff("D",Date1,DateFormat(Date2,"mm/dd/yyyy"))>
<cfif X neq DaysDiff>
<cfoutput>
<tr>
<td>
#DateFormat(Date1,"mm/dd/yyyy")#
</td>
<td>
#DateFormat(Date2,"mm/dd/yyyy")#
</td>
<td>
#x#
</td>
<td>
#DaysDiff#
</td>
</tr>
</cfoutput>
<cfbreak>
</cfif>
</cfloop>
</cfloop>
</table>
jrunrandy said on Sep 11, 2003 at 11:10 AM :
This is bug 53252 and seems to be related to the 1.4.2 JVM.
clementi said on Jan 7, 2004 at 2:34 PM :
In the description above, how about this instead: "Subtracts _date1_ from _date2_ and returns the result in units of _datepart_."
spindlenine said on Mar 19, 2004 at 1:27 PM :
Somebody posted a very helpful function on one of the other forums. This function returns the number of days between two dates, and solved the problem I was having with DateDiff().

<cffunction name="fixedDateDiff"
hint="This function fixes a bug in the 1.4.2 JVM that causes DateDiff to not
work for certain dates (4/07/2003 or 4/5/2004 or 4/4/2005). I discovered
this on the Macromedia LiveDocs for DateDiff in CF 6.1, after Joe found
an issue with the vacpac submit process that occured for April dates only.

This function returns the number of days between one date and another."
returntype="numeric">
<cfargument name="data1" type="date" required="true">
<cfargument name="data2" type="date" required="true">

<cfreturn round(dateDiff('h', data1, data2) / 24)>
</cffunction>
GreyLurk said on Mar 19, 2004 at 4:04 PM :
This error arises from Daylights Savings time ( There are only 23 hours between 4/4/2004 and 4/5/2004, so it doesn't get counted as a whole day ). The Round( DateDiff ('h'... ) / 24 ) works well, though it's annoying that this hasn't been hotfixed yet.
jrunrandy said on Mar 24, 2004 at 6:35 AM :
Go to http://www.macromedia.com/support/coldfusion/ts/documents/duplicate_hotfix.htm to get a hotfix that addresses the daylight savings time bug.
No screen name said on Aug 3, 2004 at 3:39 AM :
Installed the hotfix, there's still a 1 hour time difference for datediff between CF5 and MX server.
No screen name said on Aug 3, 2004 at 4:34 AM :
I used this code to test:

setlocale("dutch (standard)");
writeoutput(DateDiff('s', CreateDateTime(1970,1,1,0,0,0), CreateDateTime(2004,6,6,0,0,0)));

Results:
CF5: 1086480000
CFMX unpatched: 1086476400
CFMX with hotfix 53813: 1086476400

There's still 1 hour time difference
guintato said on Aug 8, 2004 at 12:01 PM :
It seems to me that DateDiff doesn't consider the time when calculating years (similar results occur for months, and weeks):

<cfscript>
dtCompDate = CreateDateTime(2002,11,5,17,45,0);
dtToday = CreateDateTime(2004,11,5,17,44,0);
iYears = DateDiff("yyyy",dtCompDate,dtToday);
</cfscript>

<cfoutput>
Compare Date: #DateFormat(dtCompDate,"mmmm d, yyyy")# #TimeFormat(dtCompDate,"h:m tt")#<br>
Today's Date: #DateFormat(dtToday,"mmmm d, yyyy")# #TimeFormat(dtToday,"h:m tt")#<br>
The number of years between these dates is #iYears#<br>
Expected answer is 1
</cfoutput>

Has anyone else encountered this problem, and if so how did you resolve it? Comments will be greatly appreciated.
halL said on Aug 10, 2004 at 12:51 PM :
guintato is correct.
DateDiff ignores the time when calculating differences in units greater than days.
We have filed bug number 56889 against this behavior.
TomChiv said on Mar 10, 2005 at 1:39 AM :
I agree with clementi, it is not clear when dateDiff will return negative or positive results, and the example requires dates to be entered, so it has to be run.
Something you could just look at and see 'ah right' would be better.
Maybe a table with a few example values for the two dates and the result codes.
redtopia said on Oct 23, 2006 at 9:42 AM :
startDate = CreateDateTime (2006, 10, 13, 8, 0, 0); // Oct. 13, 2006, 8:00 AM
endDate = CreateDateTime (2006, 10, 14, 0, 0, 0); // Oct. 14, 2006, 12:00 AM
ctDays = DateDiff ("d", startDate, endDate); // equals 0

endDate = CreateDateTime (2006, 10, 14, 8, 0, 0); // Oct. 14, 2006, 8:00 AM
ctDays = DateDiff ("d", startDate, endDate); // equals 1

Day differences are calculated by diffing the hours and then dividing by 24. I initially thought this function would tell me that there is a 1 day difference in my first example.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi58.htm