calculate number of months between two dates -


I was wondering if you can help.

I have two date strings and I wish to be able to calculate the number of months between them.

Note the dates are for different years.

Many thanks

Mark :)

0 markbpriv 9/27/2007 11:08:18 AM

I suppose you can cast them to DateTime structures. Then you can do something like the following

//d2 and d are your datetime structures

//we suppose that each month has 30 days

TimeSpan t = d2 - d;

double months = Math.Round(t.TotalDays /30);

It's not exact however (since not all months have thirty days)

Plese, do not forget to click "Mark as Answer" on the post that helped you. Thanx!

My blog: Scenes From A Developer Memory
0 dt008 9/27/2007 11:55:15 AM
Dim d1 As DateTime = New DateTime(200211)
Dim d2 As DateTime = New DateTime(2000101)
Dim As Integer = Math.Abs((d1.Year - d2.Year))
Dim months As Integer = ((M * 12)  + Math.Abs((d1.Month - d2.Month)))
Bashar Kokash, Blog
0 BasharKokash 9/27/2007 12:10:25 PM

greeting there here's your small function code

Public Sub subdate(ByVal date1 As String, ByVal date2 As String)

' note that the date is in the format mm/dd/yyyy you can reformat

Dim dt1 As Date = Date.Parse(date1)

Dim dt2 As Date = Date.Parse(date2)

' here we suppose the month is 30 days

YourTextBox.Text = Math.Abs(CInt(dt1.Subtract(dt2).TotalDays / 30))

'the abs is absolute value if the first is greater than the second

End Sub



hope being helpfull..........

0 SaloZaSolo 9/27/2007 12:11:46 PM


Thanks for this.

 Any idea how I can accomodate months that are not 30 days?


 Mark :)

0 markbpriv 9/27/2007 12:27:36 PM

Couldn't you just use DateDiff()


DateDiff(DateInterval.Month, date1, date2)

Life would be so much easier if we only had the source code.
0 jfmccarthy 9/27/2007 3:23:57 PM
 Public Function monthDifference(ByVal startDate As DateTime, ByVal endDate As DateTime) As Integer 

Dim t As TimeSpan = endDate - startDate
Dim difference As Integer = t.Days
Dim months As Integer = Math.Floor(difference / 30.475)
Return months

End Function

From :


Suprotim Agarwal


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0 Suprotim 9/27/2007 3:34:03 PM

  Any idea how I can accomodate months that are not 30 days?

My previous reply do exactly what you want.

Bashar Kokash, Blog
0 BasharKokash 9/27/2007 7:55:54 PM


Yes this all worked.

Thanks very much.


Mark :)

0 markbpriv 9/28/2007 4:33:25 PM

0 wsdesa 4/28/2009 7:28:31 AM

Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} I create a better and more accurate method.

Try this people it's the best.

Normal 0 false false false MicrosoftInternetExplorer4 ///

/// Methord to calculate no of months between two dates

/// Creater:- Sasanka Fernando

/// Email:-




/// number of months as an integer

public int monthDifference(DateTime startDate, DateTime endDate)


/// This is to set date to "0001/01/01"

DateTime systemStartDate = new DateTime();


/// Time span to set the total time difference

TimeSpan timeDifference;


///This if condition to avoid the minus value for Time difference

if (endDate > startDate)


timeDifference = endDate.Subtract(startDate);




timeDifference = startDate.Subtract(endDate);



///This is to generate a date from the systemStartDate 

DateTime generatedDate = systemStartDate.Add(timeDifference);


///Substract 1 because the systemStartDate is "0001/01/01"

int noOfYears = generatedDate.Year - 1;

int noOfMonths = generatedDate.Month - 1;


noOfMonths = noOfMonths + (noOfYears * 12);


return noOfMonths;

0 wsdesa 4/28/2009 8:23:11 AM

(Thread closed)