VBA - DateAdd Function



A Function, which returns a date to which a specified time interval has been added.

Syntax

DateAdd(interval,number,date) 

Parameter Description

  • Interval − A required parameter. It can take the following values.

    • d - day of the year

    • m - month of the year

    • y - year of the year

    • yyyy - year

    • w - weekday

    • ww - week

    • q - quarter

    • h - hour

    • m - minute

    • s - second

  • Number − A required parameter. It can take both positive and negative parameters.

  • Date − A required parameter. A variant or literal representing the date to which an interval is added.

Example

Private Sub Constant_demo_Click()
   ' Positive Interal
   date1 = 27-Jun-1894
   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

When you execute the above function, it produces the following output.

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
vba_date_time.htm
Advertisements