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
此範例會採用日期,並使用
DateAdd
函式來顯示未來指定月份數的對應日期。
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)
您可以使用 函 DateAdd
式,從日期加入或減去指定的時間間隔。 例如,您可以計算從今天起算的日期 30 天,或從現在算起 45 分鐘的時間。
若要將天數新增至 DateValue
,您可以使用 DateInterval.Day
、 DateInterval.DayOfYear
或 DateInterval.Weekday
。 這些會被視為相等,因為 DayOfYear
和 Weekday
不是有意義的時間間隔。
函式 DateAdd
永遠不會傳回無效的日期。 如有必要,結果日期的日期部分會向下調整為產生的月份最後一天。 下列範例會在 1 月 31 日新增一個月:
Dim NextMonth As Date = DateAdd(DateInterval.Month, 1, #1/31/1995#)
在這裡範例中, DateAdd
會傳 #2/28/1995#
回 ,而不是 #2/31/1995#
。 如果 DateValue
為 #1/31/1996#
,則會傳回 #2/29/1996#
,因為 1996 是閏年。
DateAdd
會從 CurrentCulture 命名空間中 System.Globalization 類別的 CultureInfo 屬性使用目前的行事曆設定。 默認值CurrentCulture取決於 控制台 設定。
由於結構支援DateTime每個Date
值,因此其方法可讓您新增時間間隔中的其他選項。 例如,您可以將小數的天數四捨五入至最接近的毫秒,新增至 Date
變數,如下所示:
Dim NextTime As Date = Now ' Current date and time.
NextTime = NextTime.AddDays(3.4) ' Increment by 3 2/5 days.
自 Interval
變數可以有下列其中一個設定。
String
要新增的時間間隔單位
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
此範例會採用日期,並使用 DateAdd
函式來顯示未來指定月份數的對應日期。
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)
您可以使用 函 DateAdd
式,從日期加入或減去指定的時間間隔。 例如,您可以計算從今天起算的日期 30 天,或從現在算起 45 分鐘的時間。
若要將天數新增至 DateValue
,您可以使用 DateInterval.Day
、 DateInterval.DayOfYear
或 DateInterval.Weekday
。 這些會被視為相等,因為 DayOfYear
和 Weekday
不是有意義的時間間隔。
函式 DateAdd
永遠不會傳回無效的日期。 如有必要,結果日期的日期部分會向下調整為產生的月份最後一天。 下列範例會在 1 月 31 日新增一個月:
Dim NextMonth As Date = DateAdd(DateInterval.Month, 1, #1/31/1995#)
在這裡範例中, DateAdd
會傳 #2/28/1995#
回 ,而不是 #2/31/1995#
。 如果 DateValue
為 #1/31/1996#
,則會傳回 #2/29/1996#
,因為 1996 是閏年。
DateAdd
會從 CurrentCulture 命名空間中 System.Globalization 類別的 CultureInfo 屬性使用目前的行事曆設定。 默認值CurrentCulture取決於 控制台 設定。
由於結構支援DateTime每個Date
值,因此其方法可讓您新增時間間隔中的其他選項。 例如,您可以將小數的天數四捨五入至最接近的毫秒,新增至 Date
變數,如下所示:
Dim NextTime As Date = Now ' Current date and time.
NextTime = NextTime.AddDays(3.4) ' Increment by 3 2/5 days.
自 Interval
變數可以有下列其中一個設定。
String
要新增的時間間隔單位