VBA DateDiff Function
Written by
Reviewed by
In this Article
DateDiff Description
Returns the difference between two date values, based on the interval specified.
Simple DateDiff Examples
Here is a simple DateDiff example:
Sub DateDiff_Year()
MsgBox DateDiff("yyyy", #1/1/2019#, #8/1/2021#)
End Sub
This code will return 2. This is difference on year (indicated by “yyyy”) between 2 days. (2021 – 2019 = 2)
In the example above, changing the positions of date1 and date2.
Sub DateDiff_Year()
MsgBox DateDiff("yyyy", #8/1/2021#, #1/1/2019#)
End Sub
This code will return -2.
DateDiff Syntax
In the VBA Editor, you can type “DateDiff(” to see the syntax for the DateDiff Function:
The DateDiff function contains 5 arguments:
Interval: Time unit (Days, Months, Years, etc.). Enter as string. (ex. “m” for Month)
Setting | Description |
---|---|
yyyy | Year |
q | Quarter |
m | Month |
y | Day of Year |
d | Day |
w | Weekday |
ww | Week |
h | Hour |
n | Minute |
s | Second |
Date1, Date2: Two dates you want to use in the calculation.
FirstDayOfWeek: A constant that specifies the first day of the week. This is optional. If not specified, Sunday is assumed.
Constant | Value | Description |
---|---|---|
vbUseSystem | 0 | Use the NLS API setting. |
vbSunday | 1 | Sunday (default) |
vbMonday | 2 | Monday |
vbTuesday | 3 | Tuesday |
vbWednesday | 4 | Wednesday |
vbThursday | 5 | Thursday |
vbFriday | 6 | Friday |
vbSaturday | 7 | Saturday |
FirstWeekOfYear: A constant that specifies the first week of the year. This is optional. If not specified, the first week is assumed to be the week in which January 1 occurs.
Constant | Value | Description |
---|---|---|
vbUseSystem | 0 | Use the NLS API setting. |
vbFirstJan1 | 1 | Start with week in which January 1 occurs (default). |
vbFirstFourDays | 2 | Start with the first week that has at least four days in the new year. |
vbFirstFullWeek | 3 | Start with first full week of the year. |
Examples of Excel VBA DateDiff Function
Referencing Dates
To start, we will demonstrate different ways to reference dates using the VBA DateDiff Function.
Each of these DateDiff functions produce the same result:
Sub DateDiff_ReferenceDates()
MsgBox DateDiff("m", #4/1/2019#, #8/1/2021#)
MsgBox DateDiff("m", DateSerial(2019, 4, 1), DateSerial(2021, 8, 1))
MsgBox DateDiff("m", DateValue("April 1, 2019"), DateValue("August 1, 2021"))
End Sub
Or you can reference cells containing dates:
Sub DateDiff_ReferenceDates_Cell()
MsgBox DateDiff("m", Range("C2").Value, Range("C3").Value)
End Sub
Or create and reference date variables:
Sub DateDiff_Variable()
Dim dt1 As Date, dt2 As Date
dt1 = #4/1/2019#
dt2 = #8/1/2021#
MsgBox DateDiff("m", dt1, dt2)
End Sub
Using Different Units of Interval
Quarters
Sub DateDiff_Quarter()
MsgBox "the number of quarters: " & DateDiff("q", #1/1/2019#, #1/1/2021#)
End Sub
Months
Sub DateDiff_Month()
MsgBox "the number of months: " & DateDiff("m", #1/1/2019#, #1/1/2021#)
End Sub
Days
Sub DateDiff_Day()
MsgBox "the number of days: " & DateDiff("d", #1/1/2019#, #1/1/2021#)
End Sub
Weeks
Sub DateDiff_Week()
MsgBox "the number of weeks: " & DateDiff("w", #1/1/2019#, #1/1/2021#)
End Sub
Hours
Sub DateDiff_Hour()
Dim dt1 As Date
Dim dt2 As Date
Dim nDiff As Long
dt1 = #8/14/2019 9:30:00 AM#
dt2 = #8/14/2019 1:00:00 PM#
nDiff = DateDiff("h", dt1, dt2)
MsgBox "hours: " & nDiff
End Sub
Minutes
Sub DateDiff_Minute()
MsgBox "mins: " & DateDiff("n", #8/14/2019 9:30:00 AM#, #8/14/2019 9:35:00 AM#)
End Sub
Seconds
Sub DateDiff_Second()
MsgBox "secs: " & DateDiff("s", #8/14/2019 9:30:10 AM#, #8/14/2019 9:30:22 AM#)
End Sub