DateAdd Function |
Microsoft.VisualBasic.DateAndTime
DateAdd( interval , number , datevalue )
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 interval of time to add
An expression denoting the number of time intervals you want to add (it can be positive or negative)
Date representing the starting date to which the interval is to be added
A past or future Date that reflects the result of the addition
Returns a Date representing the result of adding (or subtracting, if number is negative) a given number of time periods to or from a given date. For instance, you can calculate the date 178 months before today's date, or the date and time 12,789 minutes from now.
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
If number is positive, the result will be in the future; if number is negative, the result will be in the past. (The meaning of "future" and "past" here is relative to datevalue .)
The DateAdd function has a built-in calendar algorithm to prevent it from returning an invalid date. For example, you can add 10 minutes to 31 December 1999 23:55, and DateAdd automatically recalculates all elements of the date to return a valid date, in this case 1 January 2000 00:05. This includes leap years; the calendar algorithm takes the presence of 29February into account for leap years .
DateAdd(DateInterval.Day, 120, #3/3/2001#) ' Returns 7/1/2001
You can check that a date is valid using the IsDate function prior to passing it as a parameter to the function.
To add a number of days to datevalue , use either the day of the year (" y " or DateInterval.DayOfYear ), the day (" d " or DateInterval.Day ), or the weekday (" w " or DateInterval.Weekday ).
DateAdd generates an error if the result does not lie in the range of dates of the Date data type.
If number contains a fractional value, it is rounded to the nearest whole number before being used in the calculation.
You can also use the members of the DateTime structure of the BCL to manipulate dates and times.
VB 6 lacks the DateInterval enumeration and therefore only accepts a string as the interval argument.
DateDiff Function