Given a Date, return the week number of the year (1-53). The week number is calculated based on the day of the year, where week 1 starts on January 1st.

Calculate the week number using a simple formula: divide the day of the year by 7 and round up. For example, day 1-7 is week 1, day 8-14 is week 2, and so on.

Method signature: public static Integer getWeekNumber(Date inputDate)

Examples:

Input: inputDate = 2026-01-05 Output: 1 Explanation: Day 5 of the year is in week 1 Input: inputDate = 2026-07-15 Output: 29 Explanation: Day 196 of the year (approximately) is in week 29 Input: inputDate = null Output: null Explanation: Return null for null input
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 Mar 2026

Contest runs March 3 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.