# u-date-picker

```ts
import '@iyulab/components/dist/components/date-picker/UDatePicker.js';
```

**Tag:** `u-date-picker`

Single-date(-time) selection with a popover calendar. `mode="date"` (default) follows the same
convention as the native `input[type=date]`: an ISO `YYYY-MM-DD` string. `mode="datetime"` adds
a time-of-day input and the value becomes a complete ISO-8601 `DateTimeOffset` string
(`YYYY-MM-DDTHH:mm:ss±HH:mm`, seconds and the browser's local UTC offset always filled in — the
value is unconditionally valid regardless of how coarse the time input was). A value set with any
other offset — a server's UTC `…Z` — is read as the instant it names and shown in the browser's local
time, so `2026-09-06T22:22:34Z` shows `2026-09-07 07:22` in Seoul and picking the next day writes the
same moment one day later. The value keeps the string it was given until the person changes it.
Form-associated.

> The calendar week starts on the locale's first day of the week (Monday in `de` `fr` `ru`, Saturday in `fa` `ar-EG`, Sunday in `en` `ko` `ja`); a browser that reports no week information gets Sunday.

**Typing a date (`mode="date"`).** The field is a text box. Type `2026-10-02`, `2026/10/2`,
`20261002`, or just `10-02` (this year), then press Enter or leave the field. The text shows as
`YYYY-MM-DD` in every browser language; `format="locale"` shows and reads the locale's numeric
order instead (`10/02/2026` in `en-US`, `02.10.2026` in `de` — ISO is still read). Clicking the
field opens the calendar and keeps the caret in the text box; ArrowDown (or Alt+ArrowDown) moves
into the calendar. Text that is not a date clears the value and reports `badInput` (the text stays
so it can be fixed); a date outside `min`/`max` is kept and reported as out of range.
In `mode="datetime"` the text box takes a time after the date (`2026-10-02 14:30`, `9:05`, seconds
dropped unless `seconds` is set) and shows `YYYY-MM-DD HH:mm`; typing only a date keeps the time
already set. 12-hour times are read too — `3:00 PM`, `3:00pm`, `p.m. 3:00`, `오후 3:00`, `午後3:00`,
`下午 3:00` (`12 AM` is midnight) — and shown in 24-hour form; the value is the same either way.
The parser and formatter are exported as `parseDate`, `parseDateTime`, `formatDateText`,
`formatDateTimeText` and `dateTextPattern`.

The calendar popover has a footer with a "Today" quick-action button (selects today's date —
in `mode="datetime"` this also sets the time to right now — disabled when today falls outside
`min`/`max`) and, when `clearable` and a value is set, a "Clear" button next to it. Picking a
day preserves whatever time-of-day was already set; only the "Today" button overrides the time.
`min`/`max` are always date-only, even in `mode="datetime"` — time-of-day is never range-checked.

### Disabled days

`min`/`max` bound the calendar; `isDateDisabled` removes days inside it — weekends, holidays, fully
booked days. It receives the ISO day and returns `true` to disable it. A disabled day is drawn with
`aria-disabled="true"` and cannot be picked (nor can "Today" when today is disabled). A typed or
assigned value on a disabled day reports `stepMismatch` ("This date is not available") — the native
date input reports the same flag for a readable day its `step` does not allow.

```js
const holidays = new Set(['2026-10-03', '2026-10-09']);
picker.isDateDisabled = (date) => {
  const day = new Date(`${date}T00:00`).getDay();
  return day === 0 || day === 6 || holidays.has(date);
};
```

### Apply to confirm

With `confirm`, choosing in the calendar does not change the value yet: a day, the time, "Today"
and "Clear value" only stage the choice, shown in the calendar, and the footer adds **Cancel** and
**Apply**. Apply commits the staged choice (empty if "Clear value" was chosen), fires `change` once
and closes; Cancel, Escape or closing the calendar any other way drops it, and the next opening
starts from the value. Typing in the text box still commits on Enter or leaving the field.

```html
<u-date-picker name="start-date" label="Start date"></u-date-picker>

<!-- Clearable with a bounded range -->
<u-date-picker name="due-date" label="Due date" clearable min="2026-01-01" max="2026-12-31"></u-date-picker>

<!-- Date + time, value is a full ISO-8601 DateTimeOffset string -->
<u-date-picker name="sent-at" label="Sent at" mode="datetime"></u-date-picker>

<!-- Day and time are applied together -->
<u-date-picker name="due-at" label="Due at" mode="datetime" confirm></u-date-picker>
```

---

## Properties

| Property | Type | Default | Reflect | Description |
|----------|------|---------|---------|-------------|
| `mode` | `'date' \| 'datetime'` | `'date'` | ✓ | `datetime` adds a time input; value becomes a full ISO-8601 `DateTimeOffset` string |
| `value` | `string` | — | — | Selected date (ISO `YYYY-MM-DD`), or full ISO-8601 datetime in `mode="datetime"` |
| `min` | `string` | — | — | Minimum selectable date (ISO `YYYY-MM-DD`, date-only in both modes) |
| `max` | `string` | — | — | Maximum selectable date (ISO `YYYY-MM-DD`, date-only in both modes) |
| `size` | `'sm'\|'md'\|'lg'` | `'md'` | ✓ | Field size, like a button's: `--u-density` (14px) for md, 6/7 of it for sm (12px) and 8/7 for lg (16px) — change the density and all three follow. A field and a button of the same size share a height |
| `chars` | `number` | — | ✓ | Characters the field holds. An unsized field draws at N characters + its clear and calendar buttons + padding; a sized one uses it as the text floor, so a narrower box overflows. Give the length of the `format` pattern for a box that fits it exactly. Same axis as `u-input`'s `chars`. N characters are measured as drawn — tabular figures and letter spacing count, which CSS `ch` leaves out |
| `clearable` | `boolean` | `false` | ✓ | Show clear button |
| `isDateDisabled` | `(date: string) => boolean` | — | — | App rule for days that cannot be chosen (ISO in, `true` = disabled). Property only; see «Disabled days» |
| `placeholder` | `string` | — | — | Placeholder text (defaults to the pattern to type, e.g. `YYYY-MM-DD` or `YYYY-MM-DD HH:mm`) |
| `format` | `'iso' \| 'locale'` | `'iso'` | ✓ | How the text box writes and reads the date part |
| `confirm` | `boolean` | `false` | ✓ | Calendar picks wait for an Apply button (see «Apply to confirm») |
| `seconds` | `boolean` | `false` | ✓ | `mode="datetime"`: enter the time to the second (`HH:mm:ss` in the text box, seconds in the time input). The value carries seconds either way |
| `disabled` | `boolean` | `false` | ✓ | Disable |
| `readonly` | `boolean` | `false` | ✓ | Read-only |
| `required` | `boolean` | `false` | ✓ | Required |
| `invalid` | `boolean` | `false` | ✓ | Validation failed |
| `name` | `string` | — | — | Form field name |
| `label` | `string` | — | — | Field label |
| `description` | `string` | — | — | Helper text |
| `descriptionDisplay` | `'inline' \| 'hint'` | — | — | Attribute `description-display`. This field only — `hint` puts the description in a tooltip beside the label, `inline` under the control. Empty: the app-wide `UField.descriptionDisplay` |
| `validationMessage` | `string` | — | — | Error text shown while `invalid` — replaces the message the browser computed (`validation-message="…"` in a template). Text only: it does not make the value invalid; `setCustomValidity()` does that |

## Events

| Event | Description |
|-------|-------------|
| `change` | Fires when the user clicks a date cell, confirms via keyboard, commits typed text (Enter or leaving the field, when the value changes), changes the time input (`mode="datetime"`, once a date is set), or clicks the clear button — with `confirm`, calendar picks fire it only when Apply commits a different value. Programmatic value assignment does not fire it. |

## Methods

| Method | Description |
|--------|--------------|
| `validate()` | Validate; sets `invalid` |
| `reset()` | Reset value |
| `focus(options?)` | Focus the trigger |
| `blur()` | Blur the trigger |

## CSS Parts

| Part | Description |
|------|-------------|
| `field` | The `u-field` element |
| `container` | The element wrapping the trigger area |
| `input` | The text box |
| `popover` | The popover element showing the calendar |
| `calendar` | The calendar container |
| `calendar-header` | The month navigation header |
| `calendar-title` | The "Month Year" title |
| `calendar-weekdays` | The weekday header row |
| `calendar-grid` | The date grid |
| `day` | A date cell button |
| `calendar-footer` | The row holding the "Today"/"Clear" quick-action buttons |
| `calendar-time` | The row holding the time-of-day input (`mode="datetime"` only) |
| `calendar-week` | One week row inside the date grid |
| `time-input` | Time-of-day input (datetime mode only) |

## CSS Custom Properties

| Property | Description |
|----------|-------------|
| `--u-date-picker-display` | Host `display` (default: inline-block). Set `block` to fill the container width in forms and grid cells |
| `--u-date-picker-width` | Host `width` (default: auto). Set `100%` where `block` alone does not stretch the host (e.g. inside a flex container) |
| `--date-picker-min-text` | Minimum width of the text area (default `4ch`). The clear and calendar buttons never squeeze the typed date below it — a picker given less width overflows its host visibly |
| `--date-picker-popover-width` | Width of the calendar popover (default: 296px, independent of trigger width — a fixed-width calendar reads more naturally) |
