Quantcast
Channel: Difference in months between two dates - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by Tommix for Difference in months between two dates

$
0
0

Simple and fast solution to count total months between 2 dates.If you want to get only different months, not counting the one that is in From date - just remove +1 from code.

public static int GetTotalMonths(DateTime From, DateTime Till)        {            int MonthDiff = 0;            for (int i = 0; i < 12; i++)            {                if (From.AddMonths(i).Month == Till.Month)                {                    MonthDiff = i + 1;                    break;                }            }            return MonthDiff;        }

Viewing all articles
Browse latest Browse all 46

Trending Articles