Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

會傳回包含已加上指定時間間隔之日期的 Variant (Date)。

語法

DateAdd ( 間隔、數位、日期 )

DateAdd 函數語法具有下列自變數:

引數

描述

interval

必要。 字串表示式是您要新增的時間間隔。

number

必要。 這是要新增間隔數的數值表達式。 日後取得日期) 或負 (取得過去) 日期可能是正面 (。

date

必要。 Variant (日期) 或常值,代表加入間隔的日期。

設定

間隔 引數 有下列設定:

設定

描述

yyyy

q

m

月份

y

一年中的一天

d

日期

w

Weekday

ww

h

n

分鐘

s

註解

您可以使用 DateAdd 函數,從日期加上或減去指定的時間間隔。 例如,您可以使用 DateAdd 來計算今天起 30 天的日期,或是從現在算起 45 分鐘的時間。

若要將天數新增至 日期,您可以使用年 (“y”) 、Day (“d”) 或 Weekday (“w”) 。

DateAdd 函數不會傳回無效的日期。 下列範例會在 1 月 31 日加上一個月:

DateAdd("m", 1, "31-Jan-95")

在此情況下, DateAdd 會 傳回 28-Feb-95,而不是 31-Feb-95。 如果 date 是 31-Jan-96,會傳回 29-Feb-96,因為 1996 是閏年。

如果計算日期早於年 100 (,則減去的年數會比 在 date) 中還要多,則會發生錯誤。

如果 number 不是長值,則在評估之前,它會四捨五入到最接近的整數。

附註:  DateAdd 的傳回值格式是由 控制台 設定決定,而不是由 date 自變數中傳遞的格式決定。

附註: 針對 日期,如果 [ 行事曆 ] 屬性設定為 [公曆],則所提供日期必須為公曆。 如果行事曆是回歷,則所提供的日期必須是回歷。 如果 month 值是名稱,名稱必須與目前的 [ 行事曆 ] 屬性設定一致。 若要將月份名稱與目前 [行事 ] 屬性設定衝突的可能性降到最低,請 ([簡短日期] 格式輸入數值) 。

查詢範例

Expression

結果​​

SELECT DateAdd (“YYYY”,1,[DateofSale]) AS Expr1 FROM ProductSales;

將 「DateofSale」 字段的日期值加上 1 年,即可傳回結果。

SELECT DateAdd (“YYYY”,-1,[DateofSale]) AS 表達式1 FROM ProductSales;

將 「DateofSale」 字段的日期值減去 1 年,即可傳回結果。

SELECT DateAdd (“d”,10,[DateofSale]) AS NewDate FROM ProductSales;

將 「DateofSale」 字段的日期值加上 10 天,即可傳回 「NewDate」欄中的結果。

SELECT DateAdd (“ww”,-1,[DateofSale]) AS NewDate FROM ProductSales;

將 「DateofSale」 字段的日期值減去 1 週 (7 天) ,即可傳回 「NewDate」數據行中的結果。

VBA 範例

附註: 下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請參閱 Access VBA 參考

此範例會以日期做為輸入,並使用 DateAdd 函數來顯示對應的日期,以指定的月數在未來顯示。

Dim FirstDate As Date    ' Declare variables.
Dim IntervalType As String
Dim Number As Integer
Dim Msg
IntervalType = "m"    ' "m" specifies months as interval.
FirstDate = InputBox("Enter a date")
Number = InputBox("Enter number of months to add")
Msg = "New date: " & _
    DateAdd(IntervalType, Number, FirstDate)
MsgBox Msg

另請參閱

選擇正確的日期函數

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×