Calculate the number of complete months between two dates. The start date is inclusive. If the start date is after the end date, return a negative number. If either date is null, return null.
Examples:
Input: startDate = Date.newInstance(2024, 1, 15), endDate = Date.newInstance(2024, 4, 15)
Output: 3
Explanation: Three complete months from January 15 to April 15
Input: startDate = Date.newInstance(2024, 1, 1), endDate = Date.newInstance(2024, 1, 31)
Output: 0
Explanation: Less than one complete month within same month
Input: startDate = Date.newInstance(2023, 6, 1), endDate = Date.newInstance(2024, 6, 1)
Output: 12
Explanation: Exactly one year equals 12 months
Input: startDate = Date.newInstance(2024, 6, 1), endDate = Date.newInstance(2024, 1, 1)
Output: -5
Explanation: When start date is after end date, return negative months
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.