DateDiff
Purpose
Returns the number of intervals between two dates.
Syntax
DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])
Argument | Description |
---|---|
interval | Mandatory. String expression that is the interval you want to use to calculate the differences between date1 and date2. See Setting description. |
date1, date2 | Mandatory. Date expressions. Two dates you want to use in the calculation. |
firstdayofweek | Optional. Constant that specifies the first day of the week. If not specified, then Sunday is assumed. See Constant Value description. |
firstweekofyear | Optional. Constant that specifies the first week of the year. If not specified, then the first week is assumed to be the week in which January 1 occurs. See Constant Value description. |
Description
The interval argument has these values:
Interval | Description |
---|---|
yyyy | Year |
q | Quarter |
m | Month |
y | Day of year |
d | Day |
w | Day of week |
ww | Week |
h | Hour |
m | Minute |
s | Second |
The firstdayofweek argument has these values:
Constant value | Description |
---|---|
vbUseSystem or 0 | Use National Language Support (NLS) API setting |
vbSunday or 1 | Sunday (default) |
vbMonday or 2 | Monday |
vbTuesday or 3 | Tuesday |
vbWednesday or 4 | Wednesday |
vbThursday or 5 | Thursday |
vbFriday or 6 | Friday |
vbSaturday or 7 | Saturday |
The firstweekofyear argument has these values:
Constant value | Description |
---|---|
vbUseSystem or 0 | Use National Language Support (NLS) API setting |
vbFirstJan1 or 1 | Start with the week in which January 1 occurs (default) |
vbFirstFourDays or 2 | Start with the week that has at least four days in the new year |
vbFirstFullWeek or 3 | Start with the first full week of the new year |
You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, to calculate the number of days between two dates, or the number of weeks between today and the end of the year.
To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). For example, DateDiff("d", date1,date2) determines the number of days between date1 and date2. You must ensure that hard coded dates are enclosed in quotation marks ("") as the function requires a text string.
When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, then DateDiff counts the number of Mondays until date2. It counts date2 but not date1.
If interval is Week ("ww"), however, then the DateDiff function 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 not date1, even if it does fall on a Sunday.
If date1 refers to a later point in time than date2, then the DateDiff function returns a negative number.
The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.
If date1 or date2 is a date literal, then the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in quotation marks (" ") and you omit the year, then the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.
When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only one day has elapsed.
Example
This example uses the DateDiff function to display the number of days between a given date and today:
Function DiffADate(theDate)
DiffADate = "Days from today: " & DateDiff("d", Now, theDate)
End Function
Function | Result |
---|---|
DateDiff("yyyy","01/01/2016","01/01/2012") | 2 |
DateDiff("q","01/01/2016","01/01/2011") | 4 |
DateDiff("m","01/01/2016","01/01/2011") | 12 |
DateDiff("y","01/01/2016","01/01/2011") | 365 |
DateDiff("d","01/01/2016","01/01/2011") | 365 |
DateDiff("w","01/01/2016","01/01/2011") | 52 |
DateDiff("ww","01/01/2016","01/01/2011") | 52 |
DateDiff("h","01/01/2016","01/01/2011") | 8760 |
DateDiff("n","01/01/2016","01/01/2011") | 525600 |
DateDiff("s","01/01/2016","01/01/2011") | 31536000 |