DateAndTime.DateAdd Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a Date
value containing a date and time value to which a specified time interval has been added.
Overloads
DateAdd(DateInterval, Double, DateTime) |
Returns a value containing a date and time value to which a specified time interval has been added. |
DateAdd(String, Double, Object) |
Returns a value containing a date and time value to which a specified time interval has been added. |
DateAdd(DateInterval, Double, DateTime)
- Source:
- DateAndTime.vb
- Source:
- DateAndTime.vb
- Source:
- DateAndTime.vb
Returns a value containing a date and time value to which a specified time interval has been added.
public:
static DateTime DateAdd(Microsoft::VisualBasic::DateInterval Interval, double Number, DateTime DateValue);
public static DateTime DateAdd (Microsoft.VisualBasic.DateInterval Interval, double Number, DateTime DateValue);
static member DateAdd : Microsoft.VisualBasic.DateInterval * double * DateTime -> DateTime
Public Function DateAdd (Interval As DateInterval, Number As Double, DateValue As DateTime) As DateTime
Parameters
- Interval
- DateInterval
Required. A DateInterval enumeration value or a string expression representing the time interval you want to add.
- Number
- Double
Required. Floating-point expression representing the number of intervals you want to add. It can be positive (to get date/time values in the future) or negative (to get date/time values in the past). It can contain a fractional part when Interval
specifies hours, minutes, or seconds. For other values of Interval
, any fractional part of Number
is ignored.
- DateValue
- DateTime
Required. An expression representing the date and time to which the interval is to be added. DateValue
itself is not changed in the calling program.
Returns
A value containing a date and time value to which a specified time interval has been added.
Exceptions
DateValue
is not coercible to Date
.
Interval
is not valid.
Calculated date is before 00:00:00 on January 1 of the year 1, or later than 23:59:59 on December 31, 9999.
Examples
This example takes a date and, using the DateAdd
function, displays a corresponding date a specified number of months in the future.
Dim dateEntered As String =
InputBox("Enter a date", DefaultResponse:=Date.Now.ToShortDateString)
Dim monthsEntered As String =
InputBox("Enter number of months to add", DefaultResponse:="12")
Dim dateValue As Date = Date.Parse(dateEntered)
Dim monthsValue As Integer = Integer.Parse(monthsEntered)
' Add the months to the date.
Dim newDate As Date = DateAdd(DateInterval.Month, monthsValue, dateValue)
' This statement has a string interval argument, and
' is equivalent to the above statement.
'Dim newDate As Date = DateAdd("m", monthsValue, dateValue)
MessageBox.Show("New date: " & newDate.ToShortDateString)
Remarks
You can use the DateAdd
function to add or subtract a specified time interval from a date. For example, you can calculate a date 30 days from today or a time 45 minutes before now.
To add days to DateValue
, you can use DateInterval.Day
, DateInterval.DayOfYear
, or DateInterval.Weekday
. These are treated as equivalent because DayOfYear
and Weekday
are not meaningful time intervals.
The DateAdd
function never returns an invalid date. If necessary, the day part of the resulting date is adjusted downward to the last day of the resulting month in the resulting year. The following example adds one month to January 31:
Dim NextMonth As Date = DateAdd(DateInterval.Month, 1, #1/31/1995#)
In this example, DateAdd
returns #2/28/1995#
, not #2/31/1995#
. If DateValue
is #1/31/1996#
, it returns #2/29/1996#
because 1996 is a leap year.
Note
DateAdd
uses the current calendar setting from the CurrentCulture property of the CultureInfo class in the System.Globalization namespace. The default CurrentCulture values are determined by Control Panel settings.
Since every Date
value is supported by a DateTime structure, its methods give you additional options in adding time intervals. For example, you can add a fractional number of days, rounded to the nearest millisecond, to a Date
variable as follows:
Dim NextTime As Date = Now ' Current date and time.
NextTime = NextTime.AddDays(3.4) ' Increment by 3 2/5 days.
The Interval
argument can have one of the following settings.
Enumeration value | String | Unit of time interval to add |
---|---|---|
DateInterval.Day |
d | Day; truncated to integral value |
DateInterval.DayOfYear |
y | Day; truncated to integral value |
DateInterval.Hour |
h | Hour; rounded to nearest millisecond |
DateInterval.Minute |
n | Minute; rounded to nearest millisecond |
DateInterval.Month |
m | Month; truncated to integral value |
DateInterval.Quarter |
q | Quarter; truncated to integral value |
DateInterval.Second |
s | Second; rounded to nearest millisecond |
DateInterval.Weekday |
w | Day; truncated to integral value |
DateInterval.WeekOfYear |
ww | Week; truncated to integral value |
DateInterval.Year |
yyyy | Year; truncated to integral value |
See also
- DateDiff
- DatePart
- Day(DateTime)
- Format(Object, String)
- Now
- Weekday(DateTime, FirstDayOfWeek)
- Year(DateTime)
- Data Type Summary (Visual Basic)
Applies to
DateAdd(String, Double, Object)
- Source:
- DateAndTime.vb
- Source:
- DateAndTime.vb
- Source:
- DateAndTime.vb
Returns a value containing a date and time value to which a specified time interval has been added.
public:
static DateTime DateAdd(System::String ^ Interval, double Number, System::Object ^ DateValue);
public static DateTime DateAdd (string Interval, double Number, object? DateValue);
public static DateTime DateAdd (string Interval, double Number, object DateValue);
static member DateAdd : string * double * obj -> DateTime
Public Function DateAdd (Interval As String, Number As Double, DateValue As Object) As DateTime
Parameters
- Interval
- String
Required. A DateInterval enumeration value or a string expression representing the time interval you want to add.
- Number
- Double
Required. Floating-point expression representing the number of intervals you want to add. Number
can be positive (to get date/time values in the future) or negative (to get date/time values in the past). It can contain a fractional part when Interval
specifies hours, minutes, or seconds. For other values of Interval
, any fractional part of Number
is ignored.
- DateValue
- Object
Required. An expression representing the date and time to which the interval is to be added. DateValue
itself is not changed in the calling program.
Returns
A value containing a date and time value to which a specified time interval has been added.
Exceptions
DateValue
is not coercible to Date
.
Interval
is not valid.
Calculated date is before 00:00:00 on January 1 of the year 1, or later than 23:59:59 on December 31, 9999.
Examples
This example takes a date and, using the DateAdd
function, displays a corresponding date a specified number of months in the future.
Dim dateEntered As String =
InputBox("Enter a date", DefaultResponse:=Date.Now.ToShortDateString)
Dim monthsEntered As String =
InputBox("Enter number of months to add", DefaultResponse:="12")
Dim dateValue As Date = Date.Parse(dateEntered)
Dim monthsValue As Integer = Integer.Parse(monthsEntered)
' Add the months to the date.
Dim newDate As Date = DateAdd(DateInterval.Month, monthsValue, dateValue)
' This statement has a string interval argument, and
' is equivalent to the above statement.
'Dim newDate As Date = DateAdd("m", monthsValue, dateValue)
MessageBox.Show("New date: " & newDate.ToShortDateString)
Remarks
You can use the DateAdd
function to add or subtract a specified time interval from a date. For example, you can calculate a date 30 days from today or a time 45 minutes before now.
To add days to DateValue
, you can use DateInterval.Day
, DateInterval.DayOfYear
, or DateInterval.Weekday
. These are treated as equivalent because DayOfYear
and Weekday
are not meaningful time intervals.
The DateAdd
function never returns an invalid date. If necessary, the day part of the resulting date is adjusted downward to the last day of the resulting month in the resulting year. The following example adds one month to January 31:
Dim NextMonth As Date = DateAdd(DateInterval.Month, 1, #1/31/1995#)
In this example, DateAdd
returns #2/28/1995#
, not #2/31/1995#
. If DateValue
is #1/31/1996#
, it returns #2/29/1996#
because 1996 is a leap year.
Note
DateAdd
uses the current calendar setting from the CurrentCulture property of the CultureInfo class in the System.Globalization namespace. The default CurrentCulture values are determined by Control Panel settings.
Since every Date
value is supported by a DateTime structure, its methods give you additional options in adding time intervals. For example, you can add a fractional number of days, rounded to the nearest millisecond, to a Date
variable as follows:
Dim NextTime As Date = Now ' Current date and time.
NextTime = NextTime.AddDays(3.4) ' Increment by 3 2/5 days.
The Interval
argument can have one of the following settings.
Enumeration value | String | Unit of time interval to add |
---|---|---|
DateInterval.Day |
d | Day; truncated to integral value |
DateInterval.DayOfYear |
y | Day; truncated to integral value |
DateInterval.Hour |
h | Hour; rounded to nearest millisecond |
DateInterval.Minute |
n | Minute; rounded to nearest millisecond |
DateInterval.Month |
m | Month; truncated to integral value |
DateInterval.Quarter |
q | Quarter; truncated to integral value |
DateInterval.Second |
s | Second; rounded to nearest millisecond |
DateInterval.Weekday |
w | Day; truncated to integral value |
DateInterval.WeekOfYear |
ww | Week; truncated to integral value |
DateInterval.Year |
yyyy | Year; truncated to integral value |
See also
- DateDiff
- DatePart
- Day(DateTime)
- Format(Object, String)
- Now
- Weekday(DateTime, FirstDayOfWeek)
- Year(DateTime)
- Data Type Summary (Visual Basic)