Hi,

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 :)

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

Dim d1 As DateTime = New DateTime(2002, 1, 1)

Dim d2 As DateTime = New DateTime(2000, 10, 1)

Dim M As Integer = Math.Abs((d1.Year - d2.Year))

Dim months As Integer = ((M * 12) + Math.Abs((d1.Month - d2.Month)))

Bashar Kokash, Blog

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

SALO ZA SOLO

Hi,

Thanks for this.

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

Cheers

Mark :)

Couldn't you just use DateDiff()

DateDiff(DateInterval.Month, date1, date2)

Life would be so much easier if we only had the source code.

Public Function monthDifference(ByVal startDate As DateTime, ByVal endDate As DateTime) As IntegerDim t As TimeSpan = endDate - startDate

Dim difference As Integer = t.Days

Dim months As Integer = Math.Floor(difference / 30.475)

Return monthsEnd Function

From : http://forums.asp.net/p/1159955/1914676.aspx

HTH,

Suprotim Agarwal-----

http://www.dotnetcurry.com

-----

ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips

markbpriv:Any idea how I can accomodate months that are not 30 days?My previous reply do exactly what you want.

Bashar Kokash, Blog

Hi,

Yes this all worked.

Thanks very much.

Cheers

Mark :)

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:- wsdesa@gmail.com

///

///

///

///

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);

}

else

{

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;

}