DateDiff Function |
Microsoft.VisualBasic.DateAndTime
DateDiff( interval, date1, date2 [, dayofweek [, weekofyear ]])
A String expression (see the first item in Section ) or a member of the DateInterval enumeration (see the second item in Section ) that specifies the units of time used to express the difference between date1 and date2
The starting and ending dates, whose difference is computed as date2 - date1
A member of the FirstDayOfWeek enum
A member of the FirstWeekOfYear enum
A Long specifying the number of time intervals between the two dates
Calculates the number of time intervals between two dates. For example, you can use the function to determine how many days there are between 1 January 1980 and 31 May 1998.
interval can be one of the following literal strings:
String | Description |
---|---|
yyyy | Year |
q | Quarter |
m | Month |
y | Day of year |
d | Day |
w | Weekday |
ww | Week |
h | Hour |
n | Minute |
s | Second |
interval can also be a member of the DateInterval enum:
Enum DateInterval Day DayOfYear Hour Minute Month Quarter Second Week Weekday WeekOfYear End Enum
To calculate the number of days between date1 and date2 , you can use either of the DateInterval constants, DayOfYear or Day , or the string literals "y" or "d" .
When interval is Weekday or "w" , DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2 . It counts date2 , but not date1 . If interval is Week or "ww" , however, DateDiff 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 it doesn't count date1 , even if it does fall on a Sunday.
The DayOfWeek argument affects calculations that use the Week or "w" and Weekday or "ww" interval settings only.
DateDiff(DateInterval.Day, #1/1/1945#, #3/3/2001#, _ FirstDayOfWeek.System, FirstWeekOfYear.System)
When working with dates, always check that a date is valid using the IsDate function prior to passing it as a function parameter.
If date1 or date2 is enclosed in double quotation marks ( " " ) and you omit the year, 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 with interval set equal to Year , or "yyyy" , returns 1 even though only a day has elapsed.
DateDiff considers the four quarters of the year to be January 1-March 31, April 1-June 30, July 1-September 30, and October 1-December 31. Consequently, when determining the number of quarters between March 31 and April 1 of the same year, for example, DateDiff returns 1, even though the latter date is only one day after the former.
If interval is Month or "m" , DateDiff simply counts the difference in the months in which the respective dates fall. For example, when determining the number of months between January 31 and February 1 of the same year, DateDiff returns 1, even though the latter date is only one day after the former.
In calculating the number of hours, minutes, or seconds between two dates, if an explicit time is not specified, DateDiff provides a default value of midnight (00:00:00).
VB 6 lacks the DateInterval enumeration and therefore only accepts a string as the interval argument.
DateAdd Function