函式將有效的日期和時間表示式轉換為型別日期。
語法
DateAdd(interval,number,date)
引數
新增一個模組,並新增以下範例程式碼 -
Private Sub Constant_demo_Click()
' Positive Interal
date1 = 01-Jan-2013
msgbox("Line 1 : " &DateAdd("yyyy",1,date1))
msgbox("Line 2 : " &DateAdd("q",1,date1))
msgbox("Line 3 : " &DateAdd("m",1,date1))
msgbox("Line 4 : " &DateAdd("y",1,date1))
msgbox("Line 5 : " &DateAdd("d",1,date1))
msgbox("Line 6 : " &DateAdd("w",1,date1))
msgbox("Line 7 : " &DateAdd("ww",1,date1))
msgbox("Line 8 : " &DateAdd("h",1,"01-Jan-2013 12:00:00"))
msgbox("Line 9 : " &DateAdd("n",1,"01-Jan-2013 12:00:00"))
msgbox("Line 10 : "&DateAdd("s",1,"01-Jan-2013 12:00:00"))
' Negative Interval
msgbox("Line 11 : " &DateAdd("yyyy",-1,date1))
msgbox("Line 12 : " &DateAdd("q",-1,date1))
msgbox("Line 13 : " &DateAdd("m",-1,date1))
msgbox("Line 14 : " &DateAdd("y",-1,date1))
msgbox("Line 15 : " &DateAdd("d",-1,date1))
msgbox("Line 16 : " &DateAdd("w",-1,date1))
msgbox("Line 17 : " &DateAdd("ww",-1,date1))
msgbox("Line 18 : " &DateAdd("h",-1,"01-Jan-2013 12:00:00"))
msgbox("Line 19 : " &DateAdd("n",-1,"01-Jan-2013 12:00:00"))
msgbox("Line 20 : " &DateAdd("s",-1,"01-Jan-2013 12:00:00"))
End Sub
執行上面範例程式碼,得到以下結果 -
Line 1 : 27/06/1895
Line 2 : 27/09/1894
Line 3 : 27/07/1894
Line 4 : 28/06/1894
Line 5 : 28/06/1894
Line 6 : 28/06/1894
Line 7 : 4/07/1894
Line 8 : 1/01/2013 1:00:00 PM
Line 9 : 1/01/2013 12:01:00 PM
Line 10 : 1/01/2013 12:00:01 PM
Line 11 : 27/06/1893
Line 12 : 27/03/1894
Line 13 : 27/05/1894
Line 14 : 26/06/1894
Line 15 : 26/06/1894
Line 16 : 26/06/1894
Line 17 : 20/06/1894
Line 18 : 1/01/2013 11:00:00 AM
Line 19 : 1/01/2013 11:59:00 AM
Line 20 : 1/01/2013 11:59:59 AM