;

How to Get the Number of Total Months Between Two Dates in C#


Tutorialsrack 10/02/2021 C#

In this article, you will learn how to get the number of total months between two dates in C#. 

Code - To Get the No. 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));
        }
    }
}
Output
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!


Related Posts



Comments

Recent Posts
Tags