# Date Picker

A button that shows the chosen date and opens a calendar to change it: a popover, a calendar and a button, put together.

Docs: https://ballpoint.st9wd.com/docs/date-picker

```bash
npx shadcn@latest add @ballpoint/date-picker
```

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

<DatePicker value={date} onValueChange={setDate} />
```

Props:

- `value / defaultValue / onValueChange`: `Date | undefined`. The chosen date, controlled or not. Make the Dates in a client component or from strings: one a server component passes arrives as the same instant, which can be another day in the browser's time zone.
- `placeholder`: `string` (default `"Pick a date"`). Shown until a date is chosen.
- `format`: `(date: Date) => string`. How the date is written on the button. The default is long, in `locale`.
- `locale`: `string` (default `"en-US"`). Names in the calendar, and the way the default format writes the date.
- `DateRangePicker value / onValueChange`: `{ from?: Date; to?: Date }`. The same for two dates.
- `Calendar props`: `disabled, startMonth, endMonth, weekStartsOn, captionLayout…`. Passed to the Calendar in the popup.
- `seed`: `string | number`. Pins the drawing.

## Example

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

export default function DatePickerDemo() {
  return <DatePicker seed="dp-demo" aria-label="Date of birth" />;
}
```

## Date range

Two months in the popup; it closes once both ends are picked.

```tsx
"use client";

import { DateRangePicker } from "@/components/ui/date-picker";

export default function DatePickerRange() {
  return <DateRangePicker defaultValue={{ from: new Date(2026, 9, 8), to: new Date(2026, 9, 15) }} defaultMonth={new Date(2026, 9, 1)} seed="dp-range" />;
}
```

## In a form

A label, a hint and a dropdown calendar for a date far from today.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { DatePicker } from "@/components/ui/date-picker";
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";

export default function DatePickerForm() {
  const [date, setDate] = useState<Date | undefined>();
  const [saved, setSaved] = useState(false);
  return (
    <form
      className="flex w-full max-w-xs flex-col gap-4"
      onSubmit={(event) => {
        event.preventDefault();
        if (date) setSaved(true);
      }}
    >
      <Field>
        <FieldLabel htmlFor="dp-form">Date of birth</FieldLabel>
        <DatePicker
          id="dp-form"
          value={date}
          onValueChange={(next) => (setDate(next), setSaved(false))}
          captionLayout="dropdown"
          startMonth={new Date(1940, 0)}
          endMonth={new Date(2026, 11)}
          defaultMonth={new Date(2000, 0)}
          seed="dpf"
          className="w-full"
        />
        <FieldDescription>Your age is worked out from this, and never shown.</FieldDescription>
      </Field>
      <div className="flex items-center gap-4">
        <Button type="submit" disabled={!date} seed="dpf-save">
          Save
        </Button>
        {saved && <span role="status">Saved.</span>}
      </div>
    </form>
  );
}
```
