Get Number of months between two dates in C#

Last Reply on Jun 08, 2012 05:02 AM By Mudassar

Posted on Jun 08, 2012 04:48 AM

private static int monthDifference(DateTime startDate, DateTime endDate)
         {
             int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
             return Math.Abs(monthsApart);
         }

 

i hve the above code but i am not gettig the correct value

You are viewing reply posted by: Mudassar on Jun 08, 2012 05:02 AM.
Posted on Jun 08, 2012 05:02 AM
 DateTime startDate = new DateTime(2011, 12, 1);
        DateTime endDate = new DateTime(2012, 12, 1);
        int months = endDate.Subtract(startDate).Days / 30;

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108