# Date Picker

A typed, segmented date field with a calendar panel for one day or a range, optionally with a time of day.

Page: https://sui.draco.dev/docs/components/date-picker

DatePicker combines a segmented field you can type into with a calendar panel. One component covers a single date, a date range, and a date with time: `mode="range"` picks a start and an end, and `showTime` adds time segments and wheels. Segment order, month and weekday names, the first day of the week and the hour cycle all follow `locale` through `Intl`.

### Example: date-picker-demo

```tsx
"use client";

import { DatePicker } from "@workspace/ui/components/date-picker";
import { Field, FieldLabel } from "@workspace/ui/components/field";
import { useId, useState } from "react";
import type { ExampleProps } from "../types";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const id = useId();
  const [date, setDate] = useState<Date | undefined>(new Date(2026, 10, 2));
  return (
    <Field className="w-60">
      <FieldLabel id={`${id}-label`}>
        {zh ? "出发日期" : "Departure"}
      </FieldLabel>
      <DatePicker
        id={id}
        aria-labelledby={`${id}-label`}
        locale={locale}
        value={date}
        onValueChange={setDate}
      />
      <p className="text-muted-foreground text-sm" aria-live="polite">
        {date ? date.toDateString() : zh ? "未选择" : "No date"}
      </p>
    </Field>
  );
}
```

## Installation

```bash
npx shadcn@latest add draco-china/sui/date-picker
```

With pnpm, Yarn or Bun, use `pnpm dlx`, `yarn dlx` or `bunx --bun` in place of `npx`, and `pnpm add`, `yarn add` or `bun add` in place of `npm install`.

Load SUI's base styles as described in [Installation](/docs/installation).

## Usage

```tsx
import { DatePicker } from "@workspace/ui/components/date-picker";

const [date, setDate] = useState<Date>();

<DatePicker aria-label="Due date" value={date} onValueChange={setDate} />;
```

Values are `Date` objects in local time. Leave out `value` and use `defaultValue` for uncontrolled use. Clearing the field, or leaving it unfinished, reports `undefined`.

## Range

`mode="range"` takes and reports `{ from?: Date; to?: Date }`. The panel shows two months. If the end comes before the start, the two are swapped once focus leaves the field or the panel closes, so an end being typed or spun never jumps; `onValueChange` only ever reports a range in order. `presets` lists quick ranges beside the calendar; a preset whose `value` is a function is read when clicked, so a range like "Next 7 days" stays current. `min` and `max` limit the calendar and mark out-of-range typed dates as invalid.

### Example: date-picker-range

```tsx
"use client";

import {
  DatePicker,
  type DateRange,
} from "@workspace/ui/components/date-picker";
import { addDays, startOfMonth } from "date-fns";
import { useState } from "react";
import type { ExampleProps } from "../types";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const [range, setRange] = useState<DateRange | undefined>();
  const today = () => new Date(new Date().setHours(0, 0, 0, 0));
  return (
    <DatePicker
      mode="range"
      className="w-80"
      aria-label={zh ? "入住日期" : "Stay"}
      locale={locale}
      value={range}
      onValueChange={setRange}
      min={today()}
      presets={[
        {
          label: zh ? "未来 7 天" : "Next 7 days",
          value: () => ({ from: today(), to: addDays(today(), 6) }),
        },
        {
          label: zh ? "未来 30 天" : "Next 30 days",
          value: () => ({ from: today(), to: addDays(today(), 29) }),
        },
        {
          label: zh ? "下个月起" : "From next month",
          value: () => ({
            from: startOfMonth(addDays(startOfMonth(today()), 32)),
            to: undefined,
          }),
        },
      ]}
    />
  );
}
```

## With time

`showTime` adds hour and minute segments, plus time wheels next to the calendar. Pass `{ seconds, step, hourCycle }` to customize it. Picking a day keeps the time already chosen. In range mode, each end has its own wheels.

### Example: date-picker-time

```tsx
"use client";

import { DatePicker } from "@workspace/ui/components/date-picker";
import type { ExampleProps } from "../types";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <DatePicker
        showTime={{ step: 15 }}
        aria-label={zh ? "会议开始" : "Meeting start"}
        locale={locale}
        defaultValue={new Date(2026, 10, 2, 9, 30)}
      />
      <DatePicker
        mode="range"
        showTime
        aria-label={zh ? "维护窗口" : "Maintenance window"}
        locale={locale}
      />
    </div>
  );
}
```

## Typing and accessibility

Each segment is a `spinbutton`. Arrow Up/Down steps it, and the minute segment moves by `step`. Digits fill the segment and move on once no further digit fits, so typing `20261102` enters a whole date. Arrow Left/Right moves between segments, mirrored in RTL. Backspace clears a segment, then moves back. In 12-hour fields, `A` and `P` set the period. Give the field a name with `aria-label` or `aria-labelledby`; `id` lands on the first segment.

`name` submits local `yyyy-mm-dd` (or `yyyy-mm-ddThh:mm`) through a hidden input; range mode submits two inputs with the same name. `disabled` and `aria-invalid` are supported.

### Example: date-picker-states

```tsx
"use client";

import { Button } from "@workspace/ui/components/button";
import { DatePicker } from "@workspace/ui/components/date-picker";
import { DirectionProvider } from "@workspace/ui/components/direction";
import { useState } from "react";
import type { ExampleProps } from "../types";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const [submitted, setSubmitted] = useState("");
  return (
    <div className="flex w-64 flex-col gap-4">
      <DatePicker
        disabled
        aria-label={zh ? "已禁用" : "Disabled"}
        locale={locale}
        defaultValue={new Date(2026, 0, 1)}
      />
      <DatePicker
        aria-label={zh ? "超出范围" : "Out of range"}
        locale={locale}
        max={new Date(2025, 11, 31)}
        defaultValue={new Date(2026, 0, 1)}
      />
      <form
        className="flex flex-col gap-2"
        onSubmit={(event) => {
          event.preventDefault();
          setSubmitted(String(new FormData(event.currentTarget).get("due")));
        }}
      >
        <DatePicker
          name="due"
          aria-label={zh ? "截止日期" : "Due date"}
          locale={locale}
        />
        <Button type="submit" variant="outline" className="self-start">
          {zh ? "提交" : "Submit"}
        </Button>
        <output className="text-muted-foreground text-sm">
          due = {JSON.stringify(submitted)}
        </output>
      </form>
      <DirectionProvider direction="rtl">
        <div dir="rtl">
          <DatePicker aria-label="RTL" locale="ar-EG" />
        </div>
      </DirectionProvider>
    </div>
  );
}
```

## On phones

On phones the panel opens in a bottom sheet sized to its content, with wheels instead of the calendar: year, month and day in the locale's order and names, then the time on the same row, as on iOS. Seconds together with AM/PM give the time a row of its own. In range mode the start's wheels sit above the end's, each captioned. The wheels keep a date within `min` and `max`; `disabledDates` only applies to the calendar.

## Labels

Chinese locales use the Chinese copy by default. Override any text with `labels`, or import `dateTimeLabels` and `dateTimeLabelsZhCN` as a starting point.

## API reference

| Prop | Type | Default / behavior |
| --- | --- | --- |
| `mode` | `"single" \| "range"` | `"single"`. |
| `value`, `defaultValue` | `Date` or `DateRange` | Depends on `mode`. |
| `onValueChange` | `(value) => void` | Reports `undefined` when empty or unfinished. |
| `showTime` | `boolean \| { seconds?, step?, hourCycle? }` | Off. |
| `presets` | `{ label, value }[]` | Range mode only. |
| `min`, `max` | `Date` | Calendar limits; out-of-range values are invalid. |
| `disabledDates` | `Matcher \| Matcher[]` | react-day-picker matchers. |
| `locale` | `string` | Runtime locale. |
| `labels` | `DateTimeLabelOverrides` | English or Chinese, by locale. |
| `name`, `id`, `disabled` | | Form name, first segment id, disabled state. |
| `prefix` | `ReactNode` | A label inside the field, before the value, as filter rows show it; still name the field with `aria-label`. |
| `calendarProps` | `Partial<PropsBase>` | Passed to Calendar. |
| `glass`, `frosted` | `boolean \| GlassConfig`, `boolean` | Applies to the field and panel. |

Exports `DatePicker`, `DatePickerProps`, `DatePickerPreset`, `DateRange`, `dateTimeLabels`, and `dateTimeLabelsZhCN`.
