CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2003
    Location
    Holland
    Posts
    146

    [RESOLVED] Calculating the months betweeen two dates

    Hello,
    I have as example:
    Dim Datum1 as string
    Dim Datum2 as string
    Dim TotaalMaanden as integer

    Datum1 ="2011-07-31"
    Datum2 = "2012-08-01"
    TotaalMaanden = datediff("m", Datum1, Datum2)
    The result given is 13
    That should be 12 full months.

    What can i do to get 12 as result?
    Herman

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Calculating the months betweeen two dates

    The reason it gives 13 rather than 12 is that the time period specified is more than 12 months. It is 367 days. You might get better accuracy by taking the number of days between the dates divided by the number of days in a year (roughly 365.25), then get the Integer of that result multiplied by 12. This may not be perfectly accurate however. Especially over an extended period of time.

    Perhaps a better thing to do is to test each date to see if it covers an entire month. Meaning that the first date would have to be the first day of a month, and the last date would have to be the last day of a month. If not, subtract from the result given by DateDiff.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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

Featured