Design a reusable date picker component for a design system. It is used for a birth date field in a sign-up form, a check-in/check-out date range on a travel booking site, and filters on analytics dashboards ("last 30 days", custom range).
Requirements:
Discuss the component API, the date model (and time zone pitfalls), calendar rendering, keyboard interaction and
focus management, positioning, and when you'd use the native <input type="date"> instead.
Date pickers are a favourite component question because they look like a grid of numbers but involve time semantics (a calendar date is not a timestamp), localisation (formats, week starts, calendars, RTL), complex keyboard interaction (a 2D grid with paging), and form integration (typed input, partial input, validation). Start by asking whether a custom picker is needed at all.
min, max, isDateUnavailable(date), minimum/maximum range length (e.g. 1–30 nights).| Concern | Target |
|---|---|
| Correctness | no off-by-one-day bugs across time zones and DST; the value means the same date for every user |
| Localisation | formats, month/weekday names, first day of week, numbering systems, RTL via Intl |
| Accessibility | APG date picker dialog pattern; WCAG 2.2 AA; typed input always available |
| Performance | open < 16 ms of script work; month change renders 42 cells cheaply; custom cell data fetched per visible month |
| Bundle | < 10 KB gzip without a heavy date library; locale data from Intl, not bundled |
| Form integration | works with native form submission (hidden ISO value) and form libraries |
<input type="date"> acceptable for simple fields (birth dates)?Assume calendar dates, Gregorian calendar with others as an extension, design-system scope.
<input type="date"> gives a localised, accessible, mobile-optimised picker for free, and its value is an ISO
YYYY-MM-DD string. Limitations: limited styling, no range mode, no per-day content (prices), no disabled specific
dates (only min/max), and display format follows the browser/OS locale rather than the app's chosen locale. Use
it for simple fields (birth date, forms in internal tools) and on mobile where appropriate; build custom for ranges,
availability and branded booking flows. A design system can offer both behind one API (variant="native").
| Part | Responsibility |
|---|---|
| Date model | plain calendar-date type and arithmetic (add days/months, compare, start of week), no time zones |
| Locale service | Intl.DateTimeFormat for names and formatting; first day of week; parsing patterns derived from formatToParts |
| Calendar state | visible month(s), focused date, selection, hover preview for ranges |
| Field state | input text, parsed value, validation errors, segments (optional segmented input) |
| Grid renderer | weeks × days table; cell states (selected, in range, today, disabled, unavailable, outside month) |
| Popover/dialog | positioning near the input, focus management, dismissal; full-screen sheet on mobile |
| Data adapter | optional per-month fetch of availability/prices |
Date objectsThe most common date picker bug: storing a picked day as a JavaScript Date at local midnight and serialising with
toISOString(). A user in UTC+5:30 picks June 10; local midnight is June 9 18:30 UTC; the server stores June 9.
Also, DST transitions make "add 1 day" via milliseconds wrong (days with 23 or 25 hours), and some zones historically
skipped midnight entirely.
The value is a calendar date: year, month, day with no time or zone.
interface CalendarDate {
year: number;
month: number; // 1-12
day: number; // 1-31
}
// Wire and form format: ISO 8601 date string "2025-06-10"
type IsoDate = `${number}-${number}-${number}`;
type DateRange = { start: CalendarDate; end: CalendarDate };
Arithmetic uses pure calendar maths (or Temporal.PlainDate, which models exactly this; Temporal now ships in
recent Firefox and Chromium versions, with a polyfill elsewhere). If a Date is unavoidable, construct it in UTC
(Date.UTC(y, m - 1, d)) and only ever read UTC fields.
"Today" depends on a time zone: compute it with Intl.DateTimeFormat(locale, { timeZone }) from the user's (or the
property's) zone. For hotel bookings, "today" and check-in dates are in the property's time zone, not the
traveller's; the API should send a timeZone or the current local date for the property.
interface DatePickerState {
inputText: string; // what the user typed
value: CalendarDate | null; // committed value (single mode)
range: { start: CalendarDate | null; end: CalendarDate | null }; // range mode
hoverDate: CalendarDate | null; // range preview
visibleMonth: { year: number; month: number }; // first visible month
focusedDate: CalendarDate; // roving focus within the grid
isOpen: boolean;
error: 'invalid' | 'before-min' | 'after-max' | 'unavailable' | 'range-too-long' | null;
}
interface DatePickerProps {
label: string;
value?: IsoDate | null;
defaultValue?: IsoDate | null;
onChange?: (value: IsoDate | null) => void;
min?: IsoDate;
max?: IsoDate;
isDateUnavailable?: (date: IsoDate) => boolean;
locale?: string; // defaults to app locale
firstDayOfWeek?: 0 | 1 | 6; // override; otherwise from locale
timeZone?: string; // for computing "today"
name?: string; // renders a hidden input with the ISO value for native forms
required?: boolean;
description?: string; // e.g. "Format: DD/MM/YYYY"
errorMessage?: string | ((error: DatePickerState['error']) => string);
renderDay?: (date: IsoDate, state: DayState) => React.ReactNode; // prices, dots
variant?: 'popover' | 'inline' | 'native';
}
interface DateRangePickerProps extends Omit<DatePickerProps, 'value' | 'defaultValue' | 'onChange'> {
value?: { start: IsoDate; end: IsoDate } | null;
onChange?: (range: { start: IsoDate; end: IsoDate } | null) => void;
minNights?: number;
maxNights?: number;
visibleMonths?: 1 | 2;
presets?: Array<{ label: string; range: () => { start: IsoDate; end: IsoDate } }>;
onVisibleMonthsChange?: (months: Array<{ year: number; month: number }>) => void; // fetch availability
}
interface DayState {
isSelected: boolean;
isRangeStart: boolean;
isRangeEnd: boolean;
isInRange: boolean;
isToday: boolean;
isDisabled: boolean;
isUnavailable: boolean;
isOutsideMonth: boolean;
}
Values cross the component boundary as ISO strings: serialisable, time-zone free, and directly usable in URLs
and APIs (?checkin=2025-06-10&checkout=2025-06-14). A headless useDatePicker() / useCalendar() exposes state
and prop getters for custom layouts.
Availability API example:
GET /api/listings/123/availability?from=2025-06-01&to=2025-07-31
200 { "timeZone": "Europe/Lisbon", "days": { "2025-06-10": { "available": true, "price": 140 }, ... } }
formatToParts on a sample date gives the order of day, month and year
and the literal separators), show it as a hint ("DD/MM/YYYY"), and parse tolerant variants (single digits,
different separators). 06/07/2025 is ambiguous across locales; never guess, follow the locale.aria-describedby, with aria-invalid="true".Intl.Locale week info where
available, falling back to a small table: Monday for most of Europe, Sunday for the US, Saturday for some Middle
East locales).Intl.DateTimeFormat(locale, { weekday: 'short' }) with full names as abbr or
accessible labels.renderDay content (prices) fetched per visible month window; prefetch the next month when the user navigates.The calendar grid uses roving tabindex: exactly one day button is tabbable (the focused date).
| Key | Behaviour |
|---|---|
ArrowLeft / ArrowRight | previous/next day (mirrored in RTL) |
ArrowUp / ArrowDown | same weekday previous/next week |
Home / End | first/last day of the week (locale aware) |
PageUp / PageDown | same day previous/next month (clamped: Jan 31 → Feb 28/29) |
Shift+PageUp / Shift+PageDown | previous/next year |
Enter / Space | select the focused date (range: first press sets start, second sets end) |
Escape | close the popup, return focus to the calendar button (or input) without changing the value |
Moving focus across a month boundary changes the visible month. Disabled dates remain focusable (so users can hear that they're unavailable) but cannot be selected.
role="dialog" with aria-modal="true" in the APG example) labelled
by the visible month heading ("June 2025"); on open, focus goes to the selected date, or today, or the first
enabled date.overflow: hidden containers and modals don't clip it. Floating UI or
CSS anchor positioning (where supported) handles the geometry.<dialog> with showModal()) avoids cramped popovers and the virtual keyboard
covering the calendar; set inputmode="numeric" on the text input if users type.Escape; for ranges with Apply/Cancel, outside click cancels (doesn't commit a half
range).<table role="grid"> with <th scope="col" abbr="Monday">Mon</th> headers, cells containing
<button>s. Each button's accessible name is the full date ("Tuesday, June 10, 2025"), from
Intl.DateTimeFormat with dateStyle: 'full', plus state ("selected", "unavailable", "check-in date"). Use
aria-selected on selected cells and aria-disabled on disabled ones. Today: aria-current="date".ar-EG uses Arabic-Indic digits), and the first day of week from Intl.Intl.DateTimeFormat can format in other calendars (calendar: 'islamic-umalqura'), but
grid generation needs calendar-specific month lengths. Temporal supports calendars natively; libraries like React
Aria's @internationalized/date implement them. Scope this explicitly.min/max disable navigation buttons when the adjacent month is entirely out of bounds.TZ set to several zones,
including UTC+14 and UTC−12, and around DST transitions).Temporal.PlainDate-style model internally).Intl instead of bundling locale data, and is explicit about calendar-system scope.How would you add time selection? A separate time field (segmented hours/minutes with locale-aware 12/24h), and
the combined value becomes a PlainDateTime plus an explicit time zone, converted to an instant only when needed.
Why not use moment.js? It's in maintenance mode, mutable and large. Use Intl, small utilities (date-fns) or
Temporal (polyfilled where needed).
How do you prefetch availability efficiently? Fetch per month window when the calendar opens, cache by month, prefetch the next month on navigation, and keep stale data visible while revalidating.
How do you test time zone bugs? Run the suite under multiple TZ environment values in CI and include
dates around DST changes; assert ISO output equals the clicked date.
What does the hidden form value look like? <input type="hidden" name="checkin" value="2025-06-10">, so
non-JS form submission and server validation are straightforward.
Spotted a mistake or a rights concern? Report an issue