Difference in months between two dates in C#
Below is an example to calculate the difference in months between two dates.using System;
public class Program
{
public static void Main()
{
DateTime date1 = DateTime.Now; // Defining now Date
DateTime date2 = new DateTime(2021, 1, 31); // Defining Date of 01/31/2021
/* below is the way to calculate months */
int months = ((date1.Year - date2.Year) * 12) + date1.Month - date2.Month;
Console.WriteLine(months);
}
}
Output
13