Search Tools Links Login

The Daily Newbie - Using the DateAdd() Function

Posted: 2002-06-01
By: ArchiveBot
Viewed: 96

Filed Under:

Visual Basic 6

No attachments for this post

Explains how to use the Visual Basic DateAdd() function to add and subtract dates.

Original Author: Matt Roberts


content="text/html; charset=iso-8859-1">
Daily Newbie - 05/01/2001


Daily Newbie

“To Start Things
Off Right”

May 3,


style="margin-left:135.0pt;text-indent:-135.0pt">face="Arial">Today’s Keyword:
size="4" face="Arial"> DateAdd()

style="margin-left:135.0pt;text-indent:-135.0pt">face="Arial">Name Derived

"Date Addition"

margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Used for:         
Adding a specified time period to a date value.

margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">VB Help Description:    Returns a Variant (Date) containing a date to which a specified time interval has been added.

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

margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Syntax:         Val=DateAdd(Interval, Count, BaseDate)

margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Usage:          dtmNewDate = DateAdd("M", 8, "01/12/2000")

margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">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.

    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.

    style="margin-left:135.35pt;text-indent:-135.35pt">face="Arial">Copy & Paste Code:


      style="margin-left:135.35pt;text-indent:-135.35pt">  size="2" face="Arial">


            style="margin-left:1.25in;text-indent:.35pt;tab-stops:45.8pt 91.6pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">size="3" face="Arial">

    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 "================================"

      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)


    margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">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.

  • Comments on this post

    No comments have been added for this post.

    You must be logged in to make a comment.