September 15th, 2012, 07:11 AM
[RESOLVED] Calculating the months betweeen two dates
I have as example:
Dim Datum1 as string
Dim Datum2 as string
Dim TotaalMaanden as integer
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?
September 15th, 2012, 09:43 AM
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?
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.