# Time Picker

A typed, segmented time field with wheels, for one time or a start and end.

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

TimePicker is a segmented field you can type into, with wheel columns in a popover (a bottom sheet on phones). Values are plain `"HH:mm"` strings, or `"HH:mm:ss"` with `seconds`. They have no date or time zone, so they go straight into forms, URLs and APIs. `range` picks a start and an end.

### Example: time-picker-demo

```tsx
"use client";

import { TimePicker } from "@workspace/ui/components/time-picker";
import { useState } from "react";
import type { ExampleProps } from "../types";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const [time, setTime] = useState<string | undefined>("08:30");
  return (
    <div className="flex w-44 flex-col gap-2">
      <TimePicker
        aria-label={zh ? "闹钟" : "Alarm"}
        locale={locale}
        value={time}
        onValueChange={setTime}
      />
      <p className="text-muted-foreground text-sm" aria-live="polite">
        value = {JSON.stringify(time)}
      </p>
    </div>
  );
}
```

## Installation

```bash
npx shadcn@latest add draco-china/sui/time-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`.

## Usage

```tsx
import { TimePicker } from "@workspace/ui/components/time-picker";

const [time, setTime] = useState<string>();

<TimePicker aria-label="Alarm" value={time} onValueChange={setTime} />;
```

The value is always 24-hour, even when the field shows a 12-hour clock.

## Range

`range` takes and reports `{ from?: string; to?: string }`. Each end has its own segments and wheels; on phones the start's wheels sit above the end's, as in Date Picker. The ends keep the order they were set in: an end before the start crosses midnight, as in `22:00 ~ 02:00`.

### Example: time-picker-range

```tsx
"use client";

import {
  TimePicker,
  type TimeRange,
} from "@workspace/ui/components/time-picker";
import { useState } from "react";
import type { ExampleProps } from "../types";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  const [hours, setHours] = useState<TimeRange | undefined>({
    from: "09:00",
    to: "18:00",
  });
  return (
    <div className="flex w-64 flex-col gap-2">
      <TimePicker
        range
        step={30}
        aria-label={zh ? "营业时间" : "Opening hours"}
        locale={locale}
        value={hours}
        onValueChange={setHours}
      />
      <p className="text-muted-foreground text-sm" aria-live="polite">
        value = {JSON.stringify(hours)}
      </p>
    </div>
  );
}
```

## Options

`seconds` adds a seconds segment and wheel. `step` sets the minute step for the wheel and the arrow keys; typed minutes stay exact. `hourCycle` (`"h12"` or `"h23"`) overrides the locale and adds an AM/PM segment and wheel in 12-hour mode.

### Example: time-picker-options

```tsx
"use client";

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

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  return (
    <div className="flex w-48 flex-col gap-4">
      <TimePicker
        seconds
        aria-label={zh ? "精确到秒" : "With seconds"}
        locale={locale}
        defaultValue="12:00:30"
      />
      <TimePicker
        hourCycle="h12"
        step={5}
        aria-label={zh ? "12 小时制" : "12-hour clock"}
        locale={locale}
        defaultValue="19:45"
      />
      <TimePicker
        hourCycle="h23"
        aria-label={zh ? "24 小时制" : "24-hour clock"}
        locale={locale}
      />
    </div>
  );
}
```

## Typing and accessibility

Typing works as in [Date Picker](/docs/components/date-picker#accessibility): digits fill segments, arrows step them, and Backspace clears them. `name` submits the time string through a hidden input (two inputs in range mode). Supports `disabled`, `aria-invalid`, `labels`, `glass`, and `frosted`.

## API reference

| Prop | Type | Default / behavior |
| --- | --- | --- |
| `range` | `boolean` | `false`. |
| `value`, `defaultValue` | `string` or `TimeRange` | Depends on `range`. |
| `onValueChange` | `(value) => void` | Reports `undefined` when empty or unfinished. |
| `seconds` | `boolean` | `false`. |
| `step` | `number` | `1` minute. |
| `hourCycle` | `"h12" \| "h23"` | Follows `locale`. |
| `locale`, `labels` | `string`, `DateTimeLabelOverrides` | As in Date Picker. |
| `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`. |
| `glass`, `frosted` | `boolean \| GlassConfig`, `boolean` | Applies to the field and wheels. |

Exports `TimePicker`, `TimePickerProps`, `TimeValue`, `TimeRange`, `HourCycle`, `dateTimeLabels`, and `dateTimeLabelsZhCN`.
