6

In my trigger i would like to calculate the number of months between two date.

For Example if the date format is (DAY/MONTH/YEAR):

START DATE : 07/10/2013

END DATE: 12/01/2014

Number Months=4 because (07/01/2014) plus 1 month for the 5 days

I've tried with many examples, but nothing.

Please can you help me?

Thanks in advantage for any advice.

BR

13

There are a lot of ways you could tackle this. What have you tried to solve this problem so far? How about using the monthsBetween function, and then adding your requirement about a small number of days difference counting as an additional month?

Date a = Date.newInstance(2013,10,7);
Date b = Date.newInstance(2014,1,12);
Integer monthDiff = a.monthsBetween(b);
if (b.day() > a.day()) monthDiff++;
System.debug(monthDiff);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.