DateDiff

Description

Returns the number of intervals, in units of type datepart, by which date1 is less than date2.

Category

Date and time functions

Syntax


DateDiff(datepart, date1, date2) 

See also

DateAdd, DatePart, CreateTimeSpan

Parameters

Parameter
Description
datepart
One of the following strings:
  • yyyy    Year
  • q    Quarter
  • m    Month
  • y    Day of year
  • d    Day
  • w    Weekday
  • ww    Week
  • h    Hour
  • n    Minute
  • s    Second
date1
Date/time object in the range 100 AD-9999 AD
date2
Date/time object in the range 100 AD-9999 AD

Usage

To find the number of days between date1 and date2, use Day of Year or Day.

When datepart is Weekday, DateDiff returns the number of weeks between the two 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 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 falls on a Sunday.

If Date1 refers to a later date than date2, DateDiff returns a negative number.

When passing date/time value as a string, enclose it in quotes. Otherwise, it is interpreted as a number representation of a date/time object.

Year values 0 - 29 are interpreted as 21st century dates. Year values 30 - 99 are interpreted as 20th century dates.

Example

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

...



Banner.Novgorod.Ru