Skip to main content

DateAdd

Returns a date to which a specified time interval has been added.

Synopsis

DateAdd(interval,number,date)

Arguments

interval A string expression code that specifies the interval type you want to add, specified as a quoted string. The table of available codes is shown below.
number A numeric expression that is the number of intervals you want to add. The numeric expression can either be positive to add intervals, or negative to subtract intervals.
date Variable name or literal representing the date to which the specified interval is added (or subtracted). The date can optionally have a time component; if not specified, the time defaults to 00:00:00.

The DateAdd function returns the calculated date in the following format:

mm/dd/yyyy

Leading zeros are displayed. The year is displayed as four digits.

Description

The interval argument can have the following values:

Setting Description
yyyy Year
q Quarter
m Month
y Day of Year (this adds the specified number of days to date; same as “d”).
d Day
w Weekday (this adds the specified number of days to date; same as “d”).
ww Week (this adds the specified number of weeks to date).
h Hour
n Minute (note this is “n”, not “m”)
s Second

You can use the DateAdd function to add or subtract a specified interval from a date. For example, you can use DateAdd to calculate a date 30 days from a given date, or a date 100 hours earlier than a given date. To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").

The DateAdd function computes the varying number of days in different months (including leap years), and avoids returning an invalid date.

Examples

The following example adds one of each date interval unit to January 31, 2005:

NewDay = DateAdd("d",1,"31-Jan-2005")
NewWDay = DateAdd("w",1,"31-Jan-2005")
NewWeek = DateAdd("ww",1,"31-Jan-2005")
NewMonth = DateAdd("m",1,"31-Jan-2005")
NewQuarter = DateAdd("q",1,"31-Jan-2005")
NewYDay = DateAdd("y",1,"31-Jan-2005")
NewYear = DateAdd("yyyy",1,"31-Jan-2005")
Println NewDay
Println NewWDay
Println NewWeek
Println NewMonth
Println NewQuarter
Println NewYDay
Println NewYear

In the case of adding one month to 31-Jan-2005, DateAdd returns 02/28/2005, not 02/31/2005. If date is 31-Jan-2004, it returns 02/29/2005, because 2004 is a leap year. If the calculated date would precede the year 100, an error occurs.

The following example adds a time interval large enough to increment the specified date:

NewHour = DateAdd("h",27, "31-Jan-2005")
NewMin = DateAdd("n",1545, "31-Jan-2005")
NewSec = DateAdd("s",91522, "31-Jan-2005")
Println NewHour
Println NewMin
Println NewSec

Note that the values returned contain both a date and a time component.

See Also

FeedbackOpens in a new tab