calculate month differance between two dates in c# code example
Example 1: compute months c#
public static int GetMonthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}
Example 2: c# calculate difference between two dates in days
(EndDate - StartDate).TotalDays //double
(EndDate.Date - StartDate.Date).Days //int