Quantcast
Viewing all articles
Browse latest Browse all 46

Answer by Patrice Calvé for Difference in months between two dates

There's 3 cases: same year, previous year and other years.

If the day of the month does not matter...

public int GetTotalNumberOfMonths(DateTime start, DateTime end){    // work with dates in the right order    if (start > end)    {        var swapper = start;        start = end;        end = swapper;    }    switch (end.Year - start.Year)    {        case 0: // Same year            return end.Month - start.Month;        case 1: // last year            return (12 - start.Month) + end.Month;        default:            return 12 * (3 - (end.Year - start.Year)) + (12 - start.Month) + end.Month;    }}

Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>