DateAdd

The DateAdd function is used widely in Visual Basic to perform arithmetic on dates. Using a mixture of arguments, you can add any number of time intervals (day, month, year) to any valid date.

Return value As with most functions, the DateAdd function’s return value depends on the data type of the variable that it is being assigned to. The default return type is a Variant(7).

Arguments The DateAdd function has three arguments: interval, number, and date. The interval argument represents the time interval, such as day, month, or year. The number argument represents the actual number of intervals to add. This can be a negative number if you want to subtract a time interval. The date argument is the date expression on which to perform the calculation. So, for example, the following code will display the date of Christmas Day:

MsgBox DateAdd("d", 1, "12/24")

Notice that I omitted the year part of the date to demonstrate how Visual Basic will assume the current year.

Y2K issues If you try the code example above in Visual Basic 3 with your system date set to the year 2000, the message box function will display “12/24/100.” So wherever possible, try to provide a year for the date.

As with most other date functions, the Year 2000 problems for the DateAdd function start to happen when short dates are interpreted. Try the following code example in Visual Basic 3 and 4, and watch how Visual Basic jumps a hundred years:

MsgBox Format$(DateAdd("d", -1, "1/1/0"), "Long Date")

This example is simply subtracting one day from the first day of the new year. However, the result is “December 31, 1899”. Notice that the result is the same even if you use a double for your date literal (#1/1/0#) instead of a string.