Icemanind
Icemanind

Reputation: 48736

Calculating Number of Months between 2 dates

I have the following VB.NET Code:

Dim Date1 As New DateTime(2010,5,6)
Dim Date2 As New DateTime(2009,10,12)
Dim NumOfMonths = 0 ' This is where I am stumped

What I am trying to do is find out how many months are between the 2 dates. Any help would be appreciated.

Upvotes: 4

Views: 21407

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039418

Here's a method you could use:

Public Shared Function MonthDifference(ByVal first As DateTime, ByVal second As DateTime) As Integer
    Return Math.Abs((first.Month - second.Month) + 12 * (first.Year - second.Year))
End Function

like this:

Dim Date1 As New DateTime(2010,5,6)
Dim Date2 As New DateTime(2009,10,12)
Dim NumOfMonths = MonthDifference(Date1, Date2)

Upvotes: 6

Related Questions