Skip to content

Planet-Source-Code/matthew-roberts-the-daily-newbie-using-the-dateadd-function__1-22924

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

The Daily Newbie - Using the DateAdd() Function

Description

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

More Info

Submitted On
By Matthew Roberts
Level Beginner
User Rating 5.0 (30 globes from 6 users)
Compatibility VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access
Category Coding Standards
World Visual Basic
Archive File

Source Code

<title>Daily Newbie - 05/01/2001</title>

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.
  • About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published