VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



The Daily Newbie Using the DateAdd() Function

by Matthew Roberts (26 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)

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

Rate The Daily Newbie Using the DateAdd() Function



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


 


v:shapes="_x0000_s1027">


The
Daily Newbie


“To Start Things
Off Right”



          
May 3,
2001      
             


v:shapes="_x0000_s1027">


 


 





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
From:  
   
 "Date Addition" 


 


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


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
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.


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


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


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


 
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
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. 
    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. 


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




    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)




     




      style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
    margin-left:135.0pt;text-indent:-135.0pt"> 


    style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
    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.


    Download this snippet    Add to My Saved Code

  • The Daily Newbie Using the DateAdd() Function Comments

    No comments have been posted about The Daily Newbie Using the DateAdd() Function. Why not be the first to post a comment about The Daily Newbie Using the DateAdd() Function.

    Post your comment

    Subject:
    Message:
    0/1000 characters