DateDiff

Returns the number of intervals in whole units of type Datepart by which Date1 is less than Date2.

See also DateAdd, DatePart, and CreateTimeSpan.

Syntax

DateDiff(datepart, date1, date2)
datepart

One of the following strings:

date1

Date/time object in the period from 100 AD to 9999 AD.

date2

Date/time object in the period from 100 AD to 9999 AD.

Usage

If you want to know the number of days between date1 and date2, you can use either Day of Year ("y") or Day ("d").

When datepart is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.

If Date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

When passing date/time value as a string, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object returning undesired results.

Years from 0 to 29 are interpreted as 21st century values. Years 30 to 99 are interpreted as 20th century values.

Examples

<!--- 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>
...



1