#### DateDiff

##### DateDiff(intervalType, startDateTime, endDateTime, firstDayOfWeek)

Returns a number of time intervals between two specified dates.

Overload

- DateDiff(intervalType, startDateTime, endDateTime)
- DateDiff(intervalType, startDateTime, endDateTime, firstDayOfWeek)

Parameters

- intervalType - A string expression that is the interval of time you use to calculate the difference between startDateTime and endDateTime. Possible values can be:

  IntervalType value | Description
  --- | ---
  Yyyy | Year
  Q | Quarter
  M | Month
  Y | Day of year
  D | Day (both "y" and "d" find the difference in days)
  W | Number of weeks between startDateTime and endDateTime
  Ww | Number of firstDayOfWeek's between startDateTime and endDateTime
  H | Hour
  N | Minute
  S | Second

- startDateTime - The first DateTime value used in calculating the difference.
- endDateTime - The second DateTime value used in calculating the difference.
- firstDayOfWeek - An optional constant specifying the first day of the week. If not specified, jrSunday will be used.

  Constant | Description
  --- | ---
  jrUseSystem | 0 (Use the current system date)
  jrSunday | 1
  jrMonday | 2
  jrTuesday | 3
  jrWednesday | 4
  jrThursday | 5
  jrFriday | 6
  jrSaturday | 7

Return value

A Number value.

Examples

- Use DateDiff with the "d" or "y" interval type parameter to find the number of days between two dates.

  `DateDiff("d", ToDateTime(1999,10,7), ToDateTime(1999,10,10))` - Returns 3

- Use DateDiff with the "yyyy" interval type parameter to find the number of years difference between two dates. This use of DateDiff is the same as finding the difference between the year of endDateTime and the year of startDateTime.
  - `DateDiff("yyyy", ToDateTime(1999,10,7), ToDateTime(2005,2,10))` - Returns 6.
  - `DateDiff("yyyy", ToDateTime(1999,12,31), ToDateTime(2000,1,1))` - Returns 1 (a 1 year difference), even though there is only a 1 day difference between the dates.
  - `DateDiff("yyyy", ToDateTime(1999,1,1), ToDateTime(1999,12,31))` - Returns 0 (a 0 year difference), even though there is a 364 days difference.

  Suppose that for the above examples, the first date is the date that you bought a mutual fund, and the second date is the date you sold it. The mutual fund company must send you an annual report for every year in which you owned units in the fund. You would then get 7, 2 and 1 annual reports respectively, in the above cases.

- Use DateDiff with the "q" parameter to find the number of quarters (3 month periods) difference between two dates.
  - `DateDiff("q", ToDateTime(1999,10,6), ToDateTime(2003,5,20))` - Returns 14.
  - `DateDiff("q", ToDateTime(1999,3,31), ToDateTime(1999,4,1))` - Returns 1. The two dates are in adjacent quarters.
  - `DateDiff("q", ToDateTime(1999,1,1), ToDateTime(1999,3,31))` - Returns 0. The two dates are in the same quarter.

  Suppose the mutual fund company in the "yyyy" example mailed out quarterly reports. It would need to mail out 15, 2 and 1 quarterly reports respectively, in the above cases.

- Use DateDiff with the "m" parameter to find the number of months difference between two dates.

  `DateDiff("m", ToDateTime(1999,3,15), ToDateTime(1999,7,13))` - Returns 4.

- Use DateDiff with the "w" parameter to calculate the number of weeks between two dates. For example, if startDateTime is on a Tuesday, DateDiff counts the number of Tuesdays between startDateTime and endDateTime not including the initial Tuesday of startDateTime. Note however that it counts endDateTime if endDateTime is on a Tuesday.
  - `DateDiff("w", ToDateTime(1999,10,19), ToDateTime(1999,10,25))` - Returns 0.
  - `DateDiff("w", ToDateTime(1999,10,19), ToDateTime(1999, 10,26))` - Returns 1.