Given a Date value, return a new Date representing the first day of the same month and year. The day component is always 1.
Examples:
Input: d = Date.newInstance(2024, 6, 15)
Output: Date.newInstance(2024, 6, 1)
Explanation: Returns the first day of June 2024
Input: d = Date.newInstance(2024, 1, 31)
Output: Date.newInstance(2024, 1, 1)
Explanation: Returns the first day of January 2024
Input: d = Date.newInstance(2024, 12, 25)
Output: Date.newInstance(2024, 12, 1)
Explanation: Returns the first day of December 2024
Input: d = Date.newInstance(2024, 2, 29)
Output: Date.newInstance(2024, 2, 1)
Explanation: Returns the first day of February 2024 (leap year)
Input: d = Date.newInstance(2023, 11, 1)
Output: Date.newInstance(2023, 11, 1)
Explanation: Already the first day of the month, returns the same date
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 accountHow 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 June 2026
Contest runs June 1 - 30. Complete challenges to climb the leaderboard!
Only the 30 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.