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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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 Feb 2026

Contest runs February 1 - 28. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.