Determines the number of units by which date1 is less than date2.
A number of units, of type datepart.
DateDiff("datepart", "date1", "date2")
DateAdd,
DatePart,
CreateTimeSpan
New in ColdFusion MX: This function now calculates negative date differences correctly; its output may be different from that in earlier releases.
Parameter | Description |
---|---|
datepart |
String:
|
date1 |
Date/time object, in the range 100 AD-9999 AD. See "How ColdFusion processes two-digit year values". |
date2 |
Date/time object, in the range 100 AD-9999 AD. See "How ColdFusion processes two-digit year values". |
To find the number of days between date1 and date2, use DayofYear
or Day
.
When datepart is Weekday, DateDiff returns the number of weeks between the dates. If date1 falls on a Monday, DateDiff counts the number of Mondays to date2. It counts date2 but not date1.
If interval is Week, however, DateDiff returns the number of calendar weeks between the dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; it does not count date1, even if it falls on a Sunday.
If date1 refers to a later date than date2, DateDiff returns a negative number.
When passing date/time object as a string, enclose it in quotation marks. Otherwise, it is interpreted as a numeric representation of a date/time object.
<!--- This example shows the use of DateDiff ---> <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 = #type#> <cfcase value = "yyyy">years</cfcase> <cfcase value = "q">quarters</cfcase> <cfcase value = "m">months</cfcase> <cfcase value = "y">days of year</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 = #type#> <cfcase value = "yyyy">years</cfcase> <cfcase value = "q">quarters</cfcase> <cfcase value = "m">months</cfcase> <cfcase value = "y">days of year</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> ...