DateAdd Function

   
DateAdd Function

Class

Microsoft.VisualBasic.DateAndTime

Syntax

 DateAdd(   interval   ,   number   ,   datevalue   ) 
interval (required; String or DateInterval enum)

A String expression (see the first item in Section ) or a member of the DateInterval enumeration (see the second item in Section ) that specifies the interval of time to add

number (required; Double)

An expression denoting the number of time intervals you want to add (it can be positive or negative)

datevalue (required; Date, or an expression capable of conversion to a date)

Date representing the starting date to which the interval is to be added

Return Value

A past or future Date that reflects the result of the addition

Description

Returns a Date representing the result of adding (or subtracting, if number is negative) a given number of time periods to or from a given date. 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

  • interval can be one of the following literal strings:

String

Description

 yyyy 

Year

 q 

Quarter

 m 

Month

 y 

Day of year

 d 

Day

 w 

Weekday

 ww 

Week

 h 

Hour

 n 

Minute

 s 

Second

  • interval can also be a member of the DateInterval enum:

     Enum DateInterval    Day    DayOfYear    Hour    Minute    Month    Quarter    Second    Week    Weekday    WeekOfYear End Enum 
  • If number is positive, the result will be in the future; if number is negative, the result will be in the past. (The meaning of "future" and "past" here is relative to datevalue .)

  • 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. This includes leap years; the calendar algorithm takes the presence of 29February into account for leap years .

Example

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

Programming Tips and Gotchas

  • You can check that a date is valid using the IsDate function prior to passing it as a parameter to the 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 result does not lie in the range of dates of the Date data type.

  • If number contains a fractional value, it is rounded to the nearest whole number before being used in the calculation.

  • You can also use the members of the DateTime structure of the BCL to manipulate dates and times.

VB.NET/VB 6 Differences

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

See Also

DateDiff Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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