tags:

views:

549

answers:

2

Requirements:

  • Calculate the number of months between two dates: receiveDate and dueDate.
  • Both optimistic and pessimistic calculations are needed

Assumptions:

  • dueDate will always be the last day of the month.

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?

A: 

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

Sergio
+1  A: 

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.

Andy Mikula