article

The Daily Newbie - Using the DateAdd() Function

Email
Submitted on: 1/16/2015 10:04:00 PM
By: Matthew Roberts (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access
Views: 453
 
     Explains how to use the Visual Basic DateAdd() function to add and subtract dates.

 
				


Daily Newbie - 05/01/2001


The Daily Newbie

“To Start Things Off Right”

May 3, 2001

Today’s Keyword: DateAdd()

Name Derived From: "Date Addition"

Used for: Adding a specified time period to a date value.

VB Help Description: Returns a Variant (Date) containing a date to which a specified time interval has been added.

Plain English: Allows you to add a specified number of seconds, minutes, hours, days, weeks, months, quarters, or years to a date.

Syntax: Val=DateAdd(Interval, Count, BaseDate)

Usage:dtmNewDate = DateAdd("M", 8, "01/12/2000")

Parameters:

  • Interval - The unit that you want to add to the Base Date. This can be:
  • s - Seconds
  • n - Minutes
  • h - Hours
  • d - Days
  • w - Weeks
  • m - Months
  • q - Quarter
  • yyyy - Year
  • Count - The number of days, weeks, etc. that you wish to add to the date.
  • BaseDate - The date that the interval is to be added to. Example:

    To add two days to today's date:

    MsgBox DateAdd("d", 2, Date)

    If you have not read the Daily Newbie on how VB stores date format, you may want to review it now by clicking here.


    Today's code snippet prints a annual schedule of maintenance dates for a piece of equipment that must be maintained every 45 days.

    Copy & Paste Code:

    
    

    Dim dtmStartDate As Date'Holds original date Dim dtmMaintDate As Date'Holds incremented date dtmStartDate = InputBox("Enter the date of the first maintenance:") dtmMaintDate = dtmStartDate 'Start increment date at entered date Debug.Print "Maintenance Schedule for Widget" Debug.Print "================================"

    Do dtmMaintDate = DateAdd("d", 45, dtmMaintDate)
    ' Print to the debug window (press "Ctrl" Key + "G" Key ' to view the debug window
    Debug.Print dtmMaintDate ' keep going until the current maintenance date is ' greater than the start date plus one year Loop Until dtmMaintDate > DateAdd("yyyy", 1, dtmStartDate)



    Notes:

    The DateAdd function is extremely useful when you are writing time sensitive applications. You can accomplish with one function call what would take many, many lines of code without it.

    Some general notes on DateAdd:

  • Despite its name, you can subtract dates with DateAdd as well. This is accomplished by simply adding a negative number in the Count parameter.

    MsgBox DateAdd("d", -2, Date)

  • DateAdd is aware of all of the calendar weirdness such as leap years. Using it to add an interval of one day to Feb. 28, 2001 will yield Feb. 29, while it will yield March 1 for 2002.

  • Other 25 submission(s) by this author

     


    Report Bad Submission
    Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
    This submission should be removed because:

    Your Vote

    What do you think of this article (in the Beginner category)?
    (The article with your highest vote will win this month's coding contest!)
    Excellent  Good  Average  Below Average  Poor (See voting log ...)
     

    Other User Comments


     There are no comments on this submission.
     

    Add Your Feedback
    Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
     

    To post feedback, first please login.