CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] Calculating the months betweeen two dates

1. Member
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. Elite Member Power Poster
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.

#### 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

On-Demand Webinars (sponsored)

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.