In this article, you will learn how to get the number of total months between two dates in C#.
using System;
namespace Tutorialsrack
{
class Program
{
/* How to Get the Number of Total Months Between To Dates in C# */
static void Main(string[] args)
{
DateTime startDate = new DateTime(2020, 02, 05);
DateTime endDate = new DateTime(2021, 01, 05);
Console.WriteLine("Total No. of Months between Two Dates: {0}",
MonthDifference(startDate,endDate));
//Hit ENTER to exit the program
Console.ReadKey();
}
public static int MonthDifference(DateTime startDate, DateTime EndDate)
{
return Math.Abs((startDate.Month - EndDate.Month) + 12 * (startDate.Year - EndDate.Year));
}
}
}
Total No. of Months between Two Dates: 11
I hope this article will help you to understand how to get the number of total months between two dates in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments