A date field allows users to enter and edit date and time values using a keyboard. Each part of a date value is displayed in an individually editable segment.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.granularity
"day" for dates, and "minute" for times.Value
Use the value or defaultValue prop to set the date value, using objects in the @internationalized/date package. This library supports parsing date strings in multiple formats, manipulation across international calendar systems, time zones, etc.
Selected date: Monday, February 3, 2020
import {parseDate, getLocalTimeZone, type CalendarDate} from '@internationalized/date';
import {useDateFormatter} from 'react-aria';
import {DateField} from './DateField';
import {useState} from 'react';
function Example() {
let [date, setDate] = useState<CalendarDate | null>(parseDate('2020-02-03'));
let formatter = useDateFormatter({ dateStyle: 'full' });
return (
<>
<DateField
value={date}
onChange={setDate} />
<p>Selected date: {date ? formatter.format(date.toDate(getLocalTimeZone())) : '--'}</p>
</>
);
}
Format options
The date format is automatically determined based on the user's locale. DateField supports several props to control how values are displayed.
granularity
"day" for dates, and "minute" for times.International calendars
By default, DateField displays the value using the calendar system for the user's locale. Use <I18nProvider> to override the calendar system by setting the Unicode calendar locale extension. The onChange event always receives a date in the same calendar as the value or defaultValue (Gregorian if no value is provided), regardless of the displayed locale.
Forms
Use the name prop to submit the selected date to the server as an ISO 8601 string. Set the isRequired, minValue, or maxValue props to validate the value, or implement custom client or server-side validation. See the Forms guide to learn more.
import {today, getLocalTimeZone} from '@internationalized/date';
import {DateField} from './DateField';
import {Button} from './Button';
import {Form} from './Form';;
<Form>
<DateField
label="Appointment date"
name="date"
isRequired
minValue={today(getLocalTimeZone())}
/>
<Button type="submit">Submit</Button>
</Form>
API
<DateField>
<Label />
<DateInput>
{segment => <DateSegment segment={segment} />}
</DateInput>
<Text slot="description" />
<FieldError />
</DateField>
DateField
A date field allows users to enter and edit date and time values using a keyboard. Each part of a date value is displayed in an individually editable segment.
| Name | Type | Default |
|---|---|---|
isDateUnavailable | | Default: — |
| Callback that is called for each date of the calendar. If it returns true, then the date is unavailable. | ||
placeholderValue | DateValue | null | Default: — |
| A placeholder date that influences the format of the placeholder shown when no value is selected. Defaults to today's date at midnight. | ||
hourCycle | 12 | 24 | Default: — |
| Whether to display the time in 12 or 24 hour format. By default, this is determined by the user's locale. | ||
granularity | Granularity | Default: — |
Determines the smallest unit that is displayed in the date picker. By default, this is "day" for dates, and "minute" for times. | ||
hideTimeZone | boolean | Default: false
|
| Whether to hide the time zone abbreviation. | ||
shouldForceLeadingZeros | boolean | Default: — |
| Whether to always show leading zeros in the month, day, and hour fields. By default, this is determined by the user's locale. | ||
isDisabled | boolean | Default: — |
| Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
| Whether the input can be selected but not changed by the user. | ||
children | ChildrenOrFunction | Default: — |
| The children of the component. A function may be provided to alter the children based on component state. | ||
value | DateValue | null | Default: — |
| The current value (controlled). | ||
defaultValue | DateValue | null | Default: — |
| The default value (uncontrolled). | ||
onChange | | Default: — |
| Handler that is called when the value changes. | ||
Default className: react-aria-DateField
| Render Prop | CSS Selector |
|---|---|
state | CSS Selector: — |
| State of the date field. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the date field is invalid. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the date field is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the date field is read only. | |
DateInput
A date input groups the editable date segments within a date field.
| Name | Type | |
|---|---|---|
children | | |
Default className: react-aria-DateInput
| Render Prop | CSS Selector |
|---|---|
isHovered | CSS Selector: [data-hovered]
|
| Whether the date input is currently hovered with a mouse. | |
isFocusWithin | CSS Selector: [data-focus-within]
|
| Whether an element within the date input is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether an element within the date input is keyboard focused. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the date input is disabled. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the date input is invalid. | |
DateSegment
A date segment displays an individual unit of a date and time, and allows users to edit the value by typing or using the arrow keys to increment and decrement.
| Name | Type | |
|---|---|---|
segment | DateSegment | |
children | ChildrenOrFunction | |
| The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-DateSegment
| Render Prop | CSS Selector |
|---|---|
isHovered | CSS Selector: [data-hovered]
|
| Whether the segment is currently hovered with a mouse. | |
isFocused | CSS Selector: [data-focused]
|
| Whether the segment is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether the segment is keyboard focused. | |
isPlaceholder | CSS Selector: [data-placeholder]
|
| Whether the value is a placeholder. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the segment is read only. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the date field is disabled. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the date field is in an invalid state. | |
type | CSS Selector: [data-type="..."]
|
The type of segment. Values include literal, year, month, day, etc. | |
text | CSS Selector: — |
| The formatted text for the segment. | |
placeholder | CSS Selector: — |
| A placeholder string for the segment. | |