DateDiff Function

Returns the number of intervals between two dates.

Syntax

DateDiff(interval, date1, date2)

Arguments:

  • Interval: Required. String expression that is the interval you want to use to calculate the differences between date1 and date2. See Settings section for values.
  • date1, date2: Required. Date expressions. Two dates you want to use in the calculation.
Settings: The interval argument can have the following values:

Table 11-10 Interval Argument Values

Setting Description
yyyy Year
q Quarter
m Month
d Day
w Weekday
h Hour
n Minute
s Second

Supported Date Formats:

  • MM-DD-YYYY
  • MM/DD/YYYY
  • DD-MM-YYYY
  • DD/MM/YYYY
  • YYYY-MM-DD
  • YYYY/MM/DD

Supported Time Format:

hh:mm:ss

Remarks

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates.

To calculate the number of days between date1 and date2, you can use Day ("d"). If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

The following example uses the DateDiff function to display the number of days between a given date and today:

Example 1:

Function DiffADate(theDate)
DiffADate = "Days from today: " & DateDiff("d", Now, theDate)
End Function

Example 2:

Dim StartDate, EndDate, Difference
StartDate = "01/01/2025"
EndDate = "21/01/2025"
Difference = DateDiff("d", StartDate, EndDate) 
'Output: 20

Example 3:

Dim StartDate, EndDate, Difference
StartDate = "01/01/2020"
EndDate = "01/01/2025"
Difference = DateDiff("yyyy", StartDate, EndDate) 
'Output: 5

Example 4:

Dim StartDate, EndDate, Difference
StartDate = "21/01/2025"
EndDate = "21/03/2025" 
Difference = DateDiff("m", StartDate, EndDate)
'Output: 2

Example 5:

Dim StartDate, EndDate, Difference
StartDate = "21/01/2025 08:00:00" ' Includes time component.
EndDate = "21/01/2025 18:00:00"   ' Includes time component.
Difference = DateDiff("h", StartDate, EndDate) 
'Output: 10

Example 6:

Dim StartDate, EndDate, Difference
StartDate = "21/01/2025 08:00:00" ' Includes time component.
EndDate = "21/01/2025 08:45:00"   ' Includes time component.
Difference = DateDiff("n", StartDate, EndDate) 
'Output: 45

Example 7:

Dim StartDate, EndDate, Difference
StartDate = "21/01/2025 08:45:00" ' Includes time component.
EndDate = "21/01/2025 08:45:50"   ' Includes time component.
Difference = DateDiff("s", StartDate, EndDate) 
'Output: 50