Kenneth Cochran
Kenneth Cochran

Reputation: 12104

How to calculate the number of months between two DateTimes?

Requirements:

Assumptions:

I've already figured out the pessimistic calculation (meaning a single day overdue counts as a whole month:

if(receiveDate > dueDate)
    receiveDate.Month - dueDate.Month + (receiveDate.Year - dueDate.Year) * 12;

Doing a search on the internet turned up several similar examples to confirm this.

Now my instincts tell me the optimistic calculation will just be the same minus one month but for some reason it just doesn't feel right. Am I on the right track or am I missing something?

Upvotes: 2

Views: 7383

Answers (5)

daniel
daniel

Reputation: 132

int GetMonthsCount(DateTime dtStart, DateTime dtEnd)
{
    return (int)Math.Round((dtEnd - dtStart).TotalMonths);
}

Upvotes: 0

Diomos
Diomos

Reputation: 430

I am doing this extra check to get precise amount of months:

numberOfMonths = receiveDate.Month - dueDate.Month + (receiveDate.Year - dueDate.Year) * 12; if (receiveDate.Day > dueDate.Day) numberOfMonths--;

Upvotes: 0

AJB
AJB

Reputation: 21

Your formula calculates the number of months between the first of the receivedDate's month to the first of the dueDate's month. As most time elements in TimeSpan are expressed as TotalXXX, it seems weird that they left out a TotalMonths and a TotalYears.

I think it's because there aren't a fixed number of days from month to month, so it's hard to know what makes most sense in terms of how to express the fractional remainder.

My formula is this...


int nMonthDiff_FirstToFirst = DateTime.Now.Month - testDate.Month + ((DateTime.Now.Year - testDate.Year)* 12);

double dMonthDiff = (double)nMonthDiff_FirstToFirst + (DateTime.Now - testDate.AddMonths(nMonthDiff_FirstToFirst)).TotalDays / (double)DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

So what I'm doing is basically getting the month difference like you are (first of the month to first of the month) then I project my testDate into the future by the month difference. Then with that TimeSpan I get the TotalDays and divide that by the number of days in that month. Thus I'm representing the fractional portion in terms of remaining days of the month.

So if you were going May 5st 2012 -> June 3rd 2012, your formula would return 1 for month difference when a full month hasn't passed yet. In my formula the TotalDays of the projected date would yield a negative fractional number of days, and when added to the 'first to first' month difference, it would take it in as needed.

Upvotes: 0

Andy Mikula
Andy Mikula

Reputation: 16780

You're right; if you're looking for the number of complete months between the two dates, subtracting 1 (assuming the receiveDate doesn't fall on the last day of the month, in which case you will have a remainder of 0 days either way) will get you your answer.

Upvotes: 1

Sergio
Sergio

Reputation: 8269

If you don't need to keep days of month in your calculus I think it's the way to go.

Upvotes: 0

Related Questions