Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Date Input for Vue UI SDK

A date typed into month, day and year segments in the locale's order, for Vue on UI SDK, with ranges, time segments and min and max dates.

On this page9 sections

A date input splits a date into segments, month, day and year in the order the locale writes them, and each segment takes digits or steps with the arrow keys. Use it for a date people already know, such as a birth date or a contract's start, where typing is faster than paging through a calendar.

When people need to see the days of a month to choose one, use a Date Picker, which opens a calendar from a field.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Date Input anatomy: label, control, segment group, segment

Usage

<script setup lang="ts">
import { DateInput, DateInputLabel } from "@/components/ui/date-input";
</script>

DateInputLabel goes inside DateInput, where it names the group of segments, and name sends the date with a form.

<template>
  <DateInput class="w-full max-w-64" name="birthDate">
    <DateInputLabel>Date of birth</DateInputLabel>
  </DateInput>
</template>

Accessibility

Each segment is a spin button named by its unit in the field's language, such as month, day and year, or hour, minute and AM/PM once there is a time, and it reports its value, its range, and Empty while it holds nothing. Every segment is its own tab stop, and typing digits fills one and moves to the next once it is complete. The segments sit in a group that takes its name from DateInputLabel, so render one inside DateInput, or point ids at a label of your own, as the label question explains. aria-describedby on DateInput describes the same group, for a hint or an error message, and required on it marks each segment as required.

Arrow Left
Moves focus to the previous segment, or to the next one in a right-to-left page, so it always goes left on screen.
Arrow Right
Moves focus to the next segment, or to the previous one in a right-to-left page.
Arrow Up
Adds one to the focused segment, wrapping from the highest value back to the lowest. An empty year, month or day starts from today's.
Arrow Down
Takes one from the focused segment, wrapping from the lowest value to the highest.
Page Up
Adds a larger step to the focused segment: 5 years, 3 months, 7 days, 3 hours, or 15 minutes or seconds.
Page Down
Takes the same larger step away from the focused segment.
Home
Sets the focused segment to its lowest value.
End
Sets the focused segment to its highest value.
Backspace
Removes the last digit while you type, or empties the focused segment, or moves to the previous segment when it is already empty.
Delete
Empties the focused segment and keeps focus on it.
A
Sets the time to AM, on a 12-hour clock.
P
Sets the time to PM, on a 12-hour clock.
Enter
Submits the form the field sits in, as Enter does in a text input.
Tab
Moves focus to the next segment, and from the last one out of the field.

Demos

Context

A line above the segments reads the field back through DateInputContext and prints valueAsString, the date as the field formats it, or None while it is empty. The render function gets the whole input, so a readout or a hint can follow the field without a second copy of its state. Children of DateInput render before the segments, which is why the line sits on top.

Preview width

Controlled

The date lives in state: value passes it in and the change handler writes the new date back, and the line underneath prints it with toString(), as an ISO date such as 2024-04-04, or (none). A date arrives once all three segments are filled, and the value is null until then. Hold the state yourself when something else on the page reacts to the date.

Preview width

Custom separator

A range field with an arrow icon between the start and the end, passed as separator in place of the default hyphen. The separator is hidden from screen readers, which hear the two dates as two groups of segments. Use an arrow when the span should read as from and to rather than as more punctuation inside a date.

Preview width

Default value

defaultValue fills the segments on the first render with April 4, 2024, made with parseDate, and the field keeps its own state after that. Use it when the page knows a starting date and nothing else needs each change. In US English it shows as 04/04/2024, since UI SDK always writes a month or a day with two digits.

Preview width

Disabled

disabled takes every segment out of the tab order, stops typing and the arrow keys, and dims the field. Use it for a date that can't change yet, such as a return date before the trip type is chosen, and say nearby why it is locked. Its hidden input is disabled too, so the date isn't sent with the form.

Preview width

Granularity

granularity set to minute adds hour and minute segments after the date, plus AM/PM in US English, for a meeting or an appointment. hour stops at the hour, and second adds seconds. The value becomes a date with a time, so read its hour and minute along with the day.

Preview width

Invalid

invalid turns the segments red and sets aria-invalid on each, while the typed date stays in place and stays editable. Set it when validation fails, for a missing or impossible date, and put the message next to the field so color isn't the only sign. A date that min, max or isDateUnavailable rejects gets the same state by itself.

Preview width

Leading zeros

shouldForceLeadingZeros is set to false here, and UI SDK ignores it: a single-digit month or day still shows as 04, not 4. The prop is kept so code written for the other bases still compiles, which leaves this field looking and typing like the default one.

Preview width

Min max

min and max keep the date inside 2024. A date outside that window stays as typed, but the segments turn red with aria-invalid on each, and the form won't submit until the date is back inside. The field only reports the problem, so check the value again on the server.

Preview width

Range

selectionMode set to range puts a start date and an end date in one field with a hyphen between them, and the value becomes an array of two dates. With a name, each date submits as its own hidden input. Use it for a hotel stay or a billing period that people type rather than pick.

Preview width

Read only

readOnly shows April 4, 2024 and keeps every segment focusable, and Left and Right still move between them, but typing and the other arrow keys change nothing. Use it for a confirmed booking or a date copied from another record. Unlike disabled, the field isn't dimmed and its hidden input still submits.

Preview width

Root provider

useDateInput creates the field's state, with April 4, 2024 as its default, and DateInputRootProvider renders it. The Focus input button outside the field calls focus() on that same object, which moves focus to the first segment. Use the hook when a toolbar or a keyboard shortcut has to drive the field.

Preview width

Size lg

size set to lg makes the field 36 pixels tall, for a date that leads its form, such as a check-in date. Only the height changes and the text keeps its size, so set the inputs and buttons around it to their large size as well.

Preview width

Size md

md is the default size, the height of a text input, shown here to compare with the small and large fields. Leave size out to get it, and set it only when a wrapper of yours might pass another size down.

Preview width

Size sm

size set to sm makes the field 28 pixels tall, for a filter bar, a table toolbar or a dense dialog. Only the height changes and the text keeps its size, so pair it with other small controls and the row lines up.

Preview width

Time only

A formatter made with Intl.DateTimeFormat that asks only for the hour and minute, on a 12-hour clock, drops the date segments and adds AM/PM, with granularity at minute. The clock comes from the formatter's hourCycle, since the field sets no hourCycle of its own here. The value still holds today's date, so read only its hour and minute. Use it for an alarm or a shift start.

Preview width

With clear button

showClear adds a Clear date button at the end of the field while it holds a date, and the field starts at April 4, 2024 here so the button shows. Pressing it empties the value through the same change handler that typing uses and moves focus to the first segment, since the button goes away. Add it to an optional date that people often remove, such as a deadline.

Preview width

With field

A Date of birth label above the field and a hint under it asking for the local date format. DateInputLabel renders as the FieldLabel through render, so it names the group of segments and a click on it focuses the month, and aria-describedby on DateInput points the group at the hint. Use Field this way when the date sits in a form among other labeled inputs.

Preview width

FAQ

Should I use a Date Input or a Date Picker?

Use a Date Input when people already know the date and can type it faster than they could find it, such as a birth date, a passport's issue date or a contract start. Each segment takes digits, so there is nothing to open.

Use a Date Picker when people need to see the days of the month to choose, as for a booking or a delivery slot, or when the weekday matters. The picker also has a typed field of its own when you want both.

How do I get the value of a Date Input in Vue?

Bind a ref with v-model:value, or pass :value and listen to @value-change, which receives the value first, as the Controlled demo does. The value is a DateValue, or null until every segment is filled, and a range is a two-item array.

On UI SDK a DateValue is this component's own object rather than a Date, with no date library behind it, and parseDate from the same file makes one. toString() gives an ISO string such as 2024-04-04.

How do I set a minimum and maximum date on a Date Input?

Pass min and max as date values made with parseDate, as the Min max demo does with the first and last days of 2024. A date outside them stays as typed, but the segments turn red with aria-invalid and the form won't submit until it's fixed. Check it again on the server all the same.

To reject other dates, such as weekends, pass isDateUnavailable. It gets each date and the locale, and returning true marks the field invalid.

How do I add time to a Date Input?

Set granularity to hour, minute or second to add those segments after the date, as the Granularity demo does, and hourCycle to 12 or 24 to choose the clock. The value then carries a time along with the date.

For a time with no date, pass a formatter that asks only for the hour and minute, as in the Time only demo, and choose the clock in that formatter, or with hourCycle on the field, which wins when both are set. The value still holds a date, taken from today, so read only its hour and minute.

Can a Date Input take a date range?

Yes. Set selectionMode to range and the field shows a start date and an end date with a hyphen between them, as the Range demo does, and the value becomes an array of two dates. Pass separator to put something else between them, such as the arrow in the Custom separator demo.

With a name, the two dates submit as two hidden inputs under that same name, the start first, each as an ISO date such as 2024-04-04.

How do I change the Date Input format for another locale?

Set locale, such as en-GB or de-DE, and the order of the segments, the separators between them and the placeholders all follow it, so en-GB reads day, month, year. UI SDK always writes a month or a day with two digits, 04 rather than 4, so shouldForceLeadingZeros changes nothing here, as the Leading zeros demo shows. Your own formatter sets how valueAsString writes the date, and one with only the hour and minute leaves the date segments out.

How do I add a label to the Date Input?

Render DateInputLabel inside DateInput, with its own text or rendered as your FieldLabel through render, as the With field demo does. The group of segments takes its name from that label, and clicking the label focuses the first segment. Inside a Field, a plain FieldLabel names the group as well, but a label outside one isn't connected, so a screen reader hears only the segment names, with no field name.

For a label elsewhere on the page, give it an id and pass DateInput an ids object whose label function returns that id. A hint or an error message is tied the same way through aria-describedby on DateInput, which reaches the group of segments.

Does the Date Input submit with a form?

Yes, once it has a name. A hidden input carries the date as an ISO string such as 2024-04-04, with the time after a T when the field shows one, and a range sends its two dates under that one name. An empty or half-typed field sends an empty string.

A disabled field sends nothing. required on DateInput marks each segment as required and stops the submit while the date is empty. A half-typed date, or one outside min and max, stops it with or without required, and the browser shows why on the first segment.

API Reference

DateInput

PropType
value?DateValue[]

The dates: one, or a range's start and end. Pass it to control the input. Two-way with v-model.

defaultValue?DateValue[]

The dates it starts with, when uncontrolled, and goes back to when its form resets.

disabled?boolean

Whether it's disabled.

Default
false
form?string

The id of the form it belongs to, when it isn't inside it.

formatter?object

How valueAsString writes a date. Without date fields in its options (only hour and minute), the input shows a time alone.

Type
{ format: (date: Date) → string; resolvedOptions: () → Intl.ResolvedDateTimeFormatOptions }
granularity?Granularity

The smallest unit it shows: day for a date, or the time down to the hour, minute or second.

Default
"day"
hourCycle?12 | 24

A 12 or 24 hour clock for the time. Defaults to the locale's, or the formatter's.

Type
12 | 24
ids?{ label?: (index: number) → string }

The ids of the label the groups are named by, as a function of the group's index.

Type
{ label?: (index: number) → string }
invalid?boolean

Marks the input invalid, as a form's validation does.

Default
false
isDateUnavailable?(date: DateValue, locale: string) → boolean

Dates that can't be chosen. A value on one is invalid.

Type
(date: DateValue, locale: string) → boolean
Parameters

date – { add: (duration: DateDuration) → DateValue; compare: (other: DateValue) → number; day: number; hour?: number; minute?: number; month: number; second?: number; set: (fields: Partial<DateFields>) → DateValue; subtract: (duration: DateDuration) → DateValue; toDate: (timeZone?: string) → Date; toString: () → string; year: number }

locale – string

Returns
boolean
locale?string

The language whose order and separators it shows. Defaults to the page's.

Default
"en-US"
max?object

The latest date. A later one is invalid.

Type
{ add: (duration: DateDuration) → DateValue; compare: (other: DateValue) → number; day: number; hour?: number; minute?: number; month: number; second?: number; set: (fields: Partial<DateFields>) → DateValue; subtract: (duration: DateDuration) → DateValue; toDate: (timeZone?: string) → Date; toString: () → string; year: number }
min?object

The earliest date. An earlier one is invalid.

Type
{ add: (duration: DateDuration) → DateValue; compare: (other: DateValue) → number; day: number; hour?: number; minute?: number; month: number; second?: number; set: (fields: Partial<DateFields>) → DateValue; subtract: (duration: DateDuration) → DateValue; toDate: (timeZone?: string) → Date; toString: () → string; year: number }
name?string

The name each date submits under, as an ISO date.

readOnly?boolean

Whether the date input is read-only.

required?boolean

Whether a date must be entered before its form submits.

selectionMode?"single" | "range"

One date, or a range's start and end, each typed in its own group.

Type
"single" | "range"
Default
"single"
separator?string | VNode

The text or icon between a range's start and end.

Type
string | VNode
Default
"-"
shouldForceLeadingZeros?boolean

Accepted so the other bases' code compiles, but false has no effect on this base: UI SDK writes every segment's digits itself and always pads a month, a day, an hour, a minute or a second to two digits (04, not 4), with no option to drop the zero.

Default
true
showClear?boolean

Whether to show the clear button.

Default
false
size?DateInputSize

The size of the control.

Default
"md"
timeZone?string

Accepted for the other bases' API: the dates here are plain dates, in no time zone.

Default
"UTC"
EventType
@value-change(details: DateInputValueChangeDetails) → void

Runs once every segment of a date holds something, and when one is emptied.

Type
(details: DateInputValueChangeDetails) → void
Parameters

details – { value: DateValue[]; valueAsString: string[] }

Returns
void

DateInputRootProvider

PropType
valueobject

What useDateInput returned.

Type
{ clearValue: () → void; disabled: boolean; focus: () → void; invalid: boolean; placeholderValue: DateValue; setValue: (value: DateValue[]) → void; value: DateValue[]; valueAsDate: Date[]; valueAsString: string[] }
separator?string | VNode

The text or icon between a range's start and end.

Type
string | VNode
Default
"-"
showClear?boolean

Whether to show the clear button.

Default
false
size?DateInputSize

The size of the control.

Default
"md"
class?string

Merged with the part's classes.

DateInputLabel

PropType
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

useDateInput

PropType
value?DateValue[]

The dates: one, or a range's start and end. Pass it to control the input. Two-way with v-model.

defaultValue?DateValue[]

The dates it starts with, when uncontrolled, and goes back to when its form resets.

disabled?boolean

Whether the put is disabled.

form?string

The id of the form it belongs to, when it isn't inside it.

formatter?object

How valueAsString writes a date. Without date fields in its options (only hour and minute), the input shows a time alone.

Type
{ format: (date: Date) → string; resolvedOptions: () → Intl.ResolvedDateTimeFormatOptions }
granularity?Granularity

The smallest unit it shows: day for a date, or the time down to the hour, minute or second.

Default
"day"
hourCycle?12 | 24

A 12 or 24 hour clock for the time. Defaults to the locale's, or the formatter's.

Type
12 | 24
ids?{ label?: (index: number) → string }

The ids of the label the groups are named by, as a function of the group's index.

Type
{ label?: (index: number) → string }
invalid?boolean

Marks the input invalid, as a form's validation does.

isDateUnavailable?(date: DateValue, locale: string) → boolean

Dates that can't be chosen. A value on one is invalid.

Type
(date: DateValue, locale: string) → boolean
Parameters

date – { add: (duration: DateDuration) → DateValue; compare: (other: DateValue) → number; day: number; hour?: number; minute?: number; month: number; second?: number; set: (fields: Partial<DateFields>) → DateValue; subtract: (duration: DateDuration) → DateValue; toDate: (timeZone?: string) → Date; toString: () → string; year: number }

locale – string

Returns
boolean
locale?string

The language whose order and separators it shows. Defaults to the page's.

max?object

The latest date. A later one is invalid.

Type
{ add: (duration: DateDuration) → DateValue; compare: (other: DateValue) → number; day: number; hour?: number; minute?: number; month: number; second?: number; set: (fields: Partial<DateFields>) → DateValue; subtract: (duration: DateDuration) → DateValue; toDate: (timeZone?: string) → Date; toString: () → string; year: number }
min?object

The earliest date. An earlier one is invalid.

Type
{ add: (duration: DateDuration) → DateValue; compare: (other: DateValue) → number; day: number; hour?: number; minute?: number; month: number; second?: number; set: (fields: Partial<DateFields>) → DateValue; subtract: (duration: DateDuration) → DateValue; toDate: (timeZone?: string) → Date; toString: () → string; year: number }
name?string

The name each date submits under, as an ISO date.

readOnly?boolean
required?boolean

Whether a date must be entered before its form submits.

selectionMode?"single" | "range"

One date, or a range's start and end, each typed in its own group.

Type
"single" | "range"
Default
"single"
shouldForceLeadingZeros?boolean

Accepted so the other bases' code compiles, but false has no effect on this base: UI SDK writes every segment's digits itself and always pads a month, a day, an hour, a minute or a second to two digits (04, not 4), with no option to drop the zero.

Default
true
timeZone?string

Accepted for the other bases' API: the dates here are plain dates, in no time zone.

EventType
@value-change(details: DateInputValueChangeDetails) → void

Runs once every segment of a date holds something, and when one is emptied.

Type
(details: DateInputValueChangeDetails) → void
Parameters

details – { value: DateValue[]; valueAsString: string[] }

Returns
void

DateInputContext