function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Months between two dates

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.



You can use methods of Date class for calculating the number of days and months between two dates.

Please refer:

An example:
integer numberOfDays = startDate.daysBetween(endDate);
integer numOfMonths = Integer.valueOf(numberOfDays/30);

Or you can directly get no of months:

integer numberOfMonths = startDate.monthsBetween(endDate);