DateAdd Function


DateAdd Function

Class

Microsoft.VisualBasic.DateAndTime

Syntax

     Dim result As Date = DateAdd  (interval, number, dateValue) 


interval (required; String or DateInterval enumeration)

A String expression or Microsoft.VisualBasic.DateInterval enumeration item indicating which part of the date to adjust. The following table lists both the string and enumeration choices. Some intervals allow partial (decimal) values to be added to the date.

String

DateInterval

Description

d

Day

Day, to the nearest whole day

y

DayOfYear

Day, to the nearest whole day

h

Hour

Hour, to the nearest millisecond

n

Minute

Minute, to the nearest millisecond

m

Month

Month, to the nearest whole month

q

Quarter

Quarter (of a year), to the nearest whole quarter

s

Second

Second, to the nearest millisecond

w

Weekday

Day, to the nearest whole day

ww

WeekOfYear

Week, to the nearest whole week

yyyy

Year

Year, to the nearest whole year



number (required; Double)

An expression denoting the number of time intervals by which to alter the original date, either positive (to add value) or negative (to subtract value).


dateValue (required; Date or date expression)

The starting date to which the interval is to be added or subtracted.

Description

The DateAdd function adds or subtracts a component date or time value to a starting date and returns the new date. For instance, you can calculate the date 178 months before today's date, or the date and time 12,789 minutes from now.

Usage at a Glance

  • 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. Proper adjustments are made for leap years.

  • You can check that a date is valid using the IsDate 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 computed date falls outside the valid range of the Date data type.

  • The Date data type also includes members that let you manipulate the date in ways that are similar to the DateAdd function.

Example

     DateAdd(DateInterval.Day, 120, #3/3/2005#)   ' Returns 7/1/2005 

Version Differences

VB 6 lacks the DateInterval enumeration and therefore only accepts a string for the interval argument.

See Also

DateDiff Function




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net