Results 1 to 8 of 8

Thread: [RESOLVED] Add months to a date field

  1. #1

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Resolved [RESOLVED] Add months to a date field

    Hello all,

    Is there an easy way to add months to a date field?

    Example:
    This code adds 15 days to begin_date.
    ----------------------
    dim Begin_Date, End_Date as Date

    Begin_Date = Date

    End_Date = Begin_Date + 15
    ---------------------

    What I want is to add months or years to begin_date in a simple way...


    Is that possible??

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Add months to a date field

    VB Code:
    1. 'DateAdd(IntervalType, Number, FirstDate)
    2. End_date = DateAdd("d", 15, Begin_Date)
    Edit:
    Should really read peoples post.
    replace "d" with "m" for months or "yyyy" for years

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Add months to a date field

    Just use dateAdd function:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim Begin_Date As Date, End_Date As Date
    3.  
    4.     Begin_Date = Date
    5.    
    6.     'add 15 days
    7.     End_Date = DateAdd("d", 15, Begin_Date)
    8.     Debug.Print End_Date
    9.    
    10.     'add 1 month
    11.     End_Date = DateAdd("m", 1, Begin_Date)
    12.     Debug.Print End_Date
    13.    
    14.     'add 1 year
    15.     End_Date = DateAdd("yyyy", 1, Begin_Date)
    16.     Debug.Print End_Date
    17.  
    18. End Sub

  4. #4

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Re: Add months to a date field

    OK, Thanks a lot,

    Does VB have a similiar function to deal with time as well?

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Add months to a date field

    yeah, it's the same function, just define the interval accordingly

    s = seconds
    n = minutes
    h = hours

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Add months to a date field

    As usual selecting keywork and hitting F1 brings up VB's Help files.
    If you have it installed and you do that then you would see the following:
    DateAdd Function


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

    Syntax

    DateAdd(interval, number, date)

    The DateAdd function syntax has thesenamed arguments:

    Part Description
    interval Required.String expression that is the interval of time you want to add.
    number Required.Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
    date Required. Variant (Date) or literal representing date to which the interval is added.


    Settings

    The intervalargument has these settings:

    Setting Description
    yyyy Year
    q Quarter
    m Month
    y Day of year
    d Day
    w Weekday
    ww Week
    h Hour
    n Minute
    s Second


    Remarks

    You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now.

    To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").

    The DateAdd function won't return an invalid date. The following example adds one month to January 31:

    DateAdd("m", 1, "31-Jan-95")

    In this case, DateAdd returns 28-Feb-95, not 31-Feb-95. If date is 31-Jan-96, it returns 29-Feb-96 because 1996 is a leap year.

    If the calculated date would precede the year 100 (that is, you subtract more years than are in date), an error occurs.

    If number isn't aLong value, it is rounded to the nearest whole number before being evaluated.

    Note The format of the return value for DateAdd is determined by Control Panel settings, not by the format that is passed in date argument.

  7. #7

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Resolved Re: Add months to a date field

    Rhinno,

    at the moment I'm not in a pc with VB instaled... I was just curious...


    Anyway, Thanks !

  8. #8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width