Results 1 to 4 of 4

Thread: Subtract months from Date data type

  1. #1
    Steve2276
    Guest

    Question Subtract months from Date data type

    I am using the Date data type and am using the following code:

    'Calculate delete date
    Select Case RetainUnit
    Case "Days"
    dtmDeleteDate = Now() - RetainNumber
    Case "Weeks"
    dtmDeleteDate = Now() - (RetainNumber * 7)
    Case "Months"
    'dtmDeleteDate = Now() - ??????
    End Select

    The only thing is that the Date data type only seems to lend itself to maths involving whole days (integers) or hours (fractions - ie 1/24 = 1 hour).
    My question is, how can I subtract 1 month. And I don't want to subtract 30 days or whatever. So if I've got 21/05/02, then subtracting 1 month would give 21/04/02. And it works regardless of days in the month (28/29/30/31).
    Is there another data type that I should be using; or a function that I've yet to discover??
    Thanks in anticipation.

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    use the DateAdd function

    look it up in MSDN library

    i.e.

    myDate = DateAdd(-1,"m",myDate)

    would subtract 1 month from myDate

    Good Luck
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3
    Steve2276
    Guest
    Thanks.

    I actually found DateAdd in MSDN about a minute after I'd posted the thread!! -oops!
    Mind you MSDN is such a pain to navigate at times, that it could have taken me ages to get anywhere!


  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    yeah, and if you ever use the Search facility you nearly always get the maximum 500 matching search results to skim through
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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