Given a Date value, return the day number within the year (1 through 366). January 1 is day 1, December 31 is day 365 in a regular year and day 366 in a leap year. Return null for null input.
Examples:
Input: d = Date.newInstance(2024, 1, 1)
Output: 1
Explanation: January 1 is the first day of the year
Input: d = Date.newInstance(2024, 12, 31)
Output: 366
Explanation: 2024 is a leap year so December 31 is day 366
Input: d = Date.newInstance(2024, 6, 15)
Output: 167
Explanation: 31 plus 29 plus 31 plus 30 plus 31 plus 15 equals 167
Input: d = Date.newInstance(2023, 12, 31)
Output: 365
Explanation: 2023 is not a leap year so December 31 is day 365
Input: d = null
Output: null
Explanation: Null input 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.