Given a year, a month (1-12), a weekday, and an occurrence number n, return the Date of the nth occurrence of that weekday in the given month. The weekday is encoded using the ISO convention: 1 = Monday, 2 = Tuesday, ..., 7 = Sunday. Assume the requested occurrence exists in the month.

Examples:

Input: year = 2026, month = 1, weekday = 1, n = 3 Output: 2026-01-19 Explanation: The 3rd Monday of January 2026 Input: year = 2026, month = 9, weekday = 1, n = 1 Output: 2026-09-07 Explanation: The 1st Monday of September 2026 (Labor Day) Input: year = 2026, month = 11, weekday = 4, n = 4 Output: 2026-11-26 Explanation: The 4th Thursday of November 2026 (Thanksgiving) Input: year = 2026, month = 5, weekday = 7, n = 2 Output: 2026-05-10 Explanation: The 2nd Sunday of May 2026 (Mother's Day) Input: year = 2026, month = 8, weekday = 5, n = 1 Output: 2026-08-07 Explanation: The 1st Friday of August 2026
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 November 2026 (Nov 1–15)Sponsored by FLXBL

Contest runs November 1 - 15. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In