Add a specified number of months to a given date and return the resulting date. The number of months can be positive, negative, or zero. When the resulting day exceeds the number of days in the new month, the result rolls down to the last valid day of that month. Return null when the date input is null.
Examples:
Input: d = 2026-01-15, months = 3
Output: 2026-04-15
Explanation: Three months added produces April 15, 2026
Input: d = 2026-11-30, months = 3
Output: 2027-02-28
Explanation: Adding three months to November 30, 2026 lands in February 2027, clamped to the 28th
Input: d = 2026-06-15, months = -6
Output: 2025-12-15
Explanation: Subtracting six months produces December 15, 2025
Input: d = 2026-06-15, months = 0
Output: 2026-06-15
Explanation: Zero months returns the original date
Input: d = null, months = 5
Output: null
Explanation: Null date returns null
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.