All questions

Date Picker

Premium

Date Picker

Build a calendar date picker as a single React component. It is more than a styled grid: you track two separate facts — the month you are looking at (the view) and the day you have picked (the selection) — and derive the whole grid from a little date math.

Task

The starter App.tsx already renders July 2026 as a static grid (a buildMonth helper computes the day count and the leading blanks). Make it interactive:

  1. Hold two states. Keep the view in useState({ year: 2026, month: 6 }) and the selected ISO date in useState<string | null>(null). Build the grid from the view.
  2. Navigate. Wire ◀ / ▶ to step the view one month, rolling the year over at Jan/Dec.
  3. Select. On a day click, set selected to the ISO string (2026-07-15), highlight that day, and show Selected: <iso> (else No date selected) in the status line.

Examples

  • Load: the header reads "July 2026", the grid starts with three blank cells (July 1 is a Wednesday), and the status reads "No date selected".
  • Click ▶ twice: the header reads "September 2026" and the grid re-lays out for that month.
  • Click 15: it turns green and the status reads "Selected: 2026-07-15".

Notes

  • Two pieces of state, not one. The view (what month is on screen) and the selection (which day is chosen) change independently — do not fold them together.
  • The date math is two Date tricks. new Date(y, m + 1, 0).getDate() is the day count; new Date(y, m, 1).getDay() is the weekday offset for the leading blanks.
  • Selection is stored as an ISO string (YYYY-MM-DD), so it is easy to show and compare; a day highlights only when its ISO equals selected.
  • Styling (grid, cells, accent) is already in styles.css; focus on the state and the navigation.

Unlock the solution & editor

  • Runnable editor + tests

    Solve in the browser with instant Jest feedback.

  • Detailed solutions

    Walkthroughs, edge cases, and complexity notes.

  • Multi-framework variants

    React, Vue, Vanilla, Angular — same question, different stacks.

Upgrade to Premium