VBA DateDiff

Datediff Function Description

The VBA DateDiff function returns the number of time intervals between two provided Date values.

VBA DateDiff Syntax

The syntax for the DateDiff function in VBA is:

DateDiff( interval, date1, date2, [firstdayofweek], [firstweekofyear] )

Parameters

interval
A string which defines the type of date/time interval which will be returned as the unit of difference between date1 and date2. The following are acceptable interval string types:

Interval type Description
yyyy Year
q Quarter
m Month
y Day of the year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second

date1
The first date used to calculate the difference.

date2
The first date used to calculate the difference.

firstdayofweek
Optional. A constant which defines assumed first day of the week. If omitted, assumes that Sunday is the first day of the week. This parameter can be one of the following values:

VBA Constant Value Description
vbUseSystem 0 Use the system settings
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday

firstweekofyear
Optional. A constant which defines assumed first week of the year. If omitted, assumes the week of Jan 1st. Otherwise use the following values:

VBA Constant Value Description
vbUseSystem 0 Use the system setting
vbFirstJan1 1 Use the first week that includes Jan 1st (default)
vbFirstFourDays 2 Use the first week in the year that has at least 4 days
vbFirstFullWeek 3 Use the first full week of the year

Example usage

The DateDiff function can be used in VBA code. Let’s look at some VBA DateDiff function examples:

DateDiff "yyyy", "2015/10/11", "2013/10/11"
'Result: 2

DateDiff "m", "2015/10/11", "2013/10/11"
'Result: -24

DateDiff "m", "2013/10/11", "2015/10/11"
'Result: 24

DateDiff "n", "2015/10/11 14:30", "2015/10/11 15:30"
'Result: 60