Calculate number of months between two dates

Here is the function to calculate months between two dates –

        public static double GetMonths(DateTime from, DateTime to)
            if (to.Ticks < from.Ticks)
                DateTime temp = from;
                from = to;
                to = temp;

            double percFrom = (double)(from.Day-1) / DateTime.DaysInMonth(from.Year, from.Month);
            double percTo = (double)to.Day / DateTime.DaysInMonth(to.Year, to.Month);

            double months = (to.Year * 12 + to.Month) - (from.Year * 12 + from.Month);
            return months - percFrom + percTo;
Posted in Uncategorized. Leave a Comment »

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: