Calculating a Month difference between specified Years

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 810
public int CalculateMonthDifference(DateTime start_date, DateTime end_date)
{
int months_part = 12 * (start_date.Year - end_date.Year)
+ start_date.Month - end_date.Month;
return Math.Abs(months_part);
}


Now to check,just write:-
Response.Write("Month is:- " + CalculateMonthDifference(DateTime.Now, DateTime.Now.AddYears(-5)) + "</br>");
Response.Write("Month is:- " + CalculateMonthDifference(DateTime.Now, DateTime.Now.AddYears(-10)) + "</br>");
Response.Write("Month is:- " + CalculateMonthDifference(DateTime.Now.AddMonths(-5), DateTime.Now.AddYears(-5)) + "</br>");


Output:-
60
120
55

Comments or Responses

Login to post response