Quantcast
Viewing all articles
Browse latest Browse all 46

Answer by jenson-button-event for Difference in months between two dates

I just needed something simple to cater for e.g. employment dates where only the month/year is entered, so wanted distinct years and months worked in. This is what I use, here for usefullness only

public static YearsMonths YearMonthDiff(DateTime startDate, DateTime endDate) {    int monthDiff = ((endDate.Year * 12) + endDate.Month) - ((startDate.Year * 12) + startDate.Month) + 1;    int years = (int)Math.Floor((decimal) (monthDiff / 12));    int months = monthDiff % 12;    return new YearsMonths {        TotalMonths = monthDiff,            Years = years,            Months = months    };}

.NET Fiddle


Viewing all articles
Browse latest Browse all 46

Trending Articles