Below code snippets is written for Finding the number of months encompassed between the given dates:-
public int GetNoOfMonthsEncompassedByDates(DateTime StartDate, DateTime EndDate)
{
return Math.Abs(12 * (StartDate.Year - EndDate.Year) + StartDate.Month - EndDate.Month) + 1;
}
int result = GetNoOfMonthsEncompassedByDates(DateTime.Now, DateTime.Now.AddMonths(7));
Response.Write(result + "<br/>");
Output:- 8