DateAdd (function)

Syntax

DateAdd(interval$, increment&, date)

Description

Returns a Date variant representing the sum of date and a specified number (increment) of time intervals (interval$).

Comments

This function adds a specified number (increment) of time intervals (interval$) to the specified date (date). The following table describes the parameters to the DateAdd function:

 

Parameter

Description

 

Interval$

String expression indicating the time interval used in the addition.

 

Increment

Integer indicating the number of time intervals you wish to add. Positive values result in dates in the future; negative values result in dates in the past.

 

Date

Any expression convertible to a Date.

 

The interval$ parameter specifies what unit of time is to be added to the given date. It can be any of the following:

 

Time

Intervale

 

"y"

Day of the year

 

"yyyy"

Year

 

"d"

Day

 

"m"

Month

 

"q"

Quarter

 

"ww"

Week

 

"h"

Hour

 

"n"

Minute

 

"s"

Second

 

"w"

Weekday

 

To add days to a date, you may use either day, day of the year, or weekday, as they are all equivalent ("d", "y", "w").

The DateAdd function will never return an invalid date/time expression. The following example adds two months to December 31, 1992:

  s# = DateAdd("m",2,"December 31,1992")

In this example, s is returned as the double-precision number equal to "February 28, 1993", not "February 31, 1993".

A runtime error is generated if you try to subtract a time interval that is larger than the time value of the date.

Example

This example gets today's date using the Date$ function; adds three years, two months, one week, and two days to it; and then displays the result in a dialog box.

Sub Main()

  Dim sdate$
  sdate$ = Date$
  NewDate# = DateAdd("yyyy",4,sdate$)
  NewDate# = DateAdd("m",3,NewDate#)
  NewDate# = DateAdd("ww",2,NewDate#)
  NewDate# = DateAdd("d",1,NewDate#)
  s$ = "Four years, three months, two weeks, and one day from now will be: "
  s$ = s$ & Format(NewDate#,"long date")
  MsgBox s$
End Sub

See Also

DateDiff (function).

More information

D