O'Reilly logo

VBScript in a Nutshell, 2nd Edition by Ron Petrusha, Matt Childs, Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

DateAdd Function

Syntax

DateAdd(interval, number, date)
interval

Use: Required

Data Type: String

An expression denoting the interval of time you need to add or subtract (see the following table “Interval Settings”).

number

Use: Required

Data Type: Any numeric type

An expression denoting the number of time intervals you want to add or subtract.

date

Use: Required

Data Type: Date

The date on which to base the DateAdd calculation.

Interval Settings

Setting

Description

yyyy

Year

q

Quarter

m

Month

y

Day of year

d

Day

w

Weekday

ww

Week

h

Hour

n

Minute

s

Second

Return Value

A Date.

Description

Returns a Date representing the result of adding or subtracting a given number of time periods to or from a given date or time. For instance, you can calculate the date 178 months before today’s date, or the date and time 12,789 minutes from now.

Rules at a Glance

  • Specify the interval value as a string enclosed in quotation marks (e.g., "ww").

  • If number is positive, the result will be after date; if number is negative, the result will be before date.

  • The DateAdd function has a built-in calendar algorithm to prevent it from returning an invalid date. For example, if you add 10 minutes to 31 December 1999 23:55, DateAdd automatically recalculates all elements of the date to return a valid date—in this case, 1 January 2000 00:05. In addition, the calendar algorithm takes the presence of 29 February into account for leap years.

Example

Dim lNoOfIntervals
lNoOfIntervals = 100
Msgbox DateAdd("d", lNoOfIntervals, Now)

Programming ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required