A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
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/useDateFormatter';
import {DatePicker} from './DatePicker';
import {useState} from 'react';
function Example() {
let [date, setDate] = useState<CalendarDate | null>(parseDate('2020-02-03'));
let formatter = useDateFormatter({ dateStyle: 'full' });
return (
<>
<DatePicker
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. DatePicker supports several props to control how values are displayed.
International calendars
By default, DatePicker 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. The isDateUnavailable callback prevents certain dates from being selected. See the Forms guide to learn more.
import {isWeekend, today, getLocalTimeZone} from '@internationalized/date';
import {useLocale} from 'react-aria-components/I18nProvider';
import {DatePicker} from './DatePicker';
import {Button} from './Button';
import {Form} from './Form';
function Example() {
let {locale} = useLocale();
let now = today(getLocalTimeZone());
let disabledRanges = [
[now, now.add({ days: 5 })],
[now.add({ days: 14 }), now.add({ days: 16 })],
[now.add({ days: 23 }), now.add({ days: 24 })]
];
return (
<Form>
<DatePicker
label="Appointment date"
name="date"
isRequired
minValue={today(getLocalTimeZone())}
isDateUnavailable={date => (
isWeekend(date, locale) ||
disabledRanges.some((interval) =>
date.compare(interval[0]) >= 0 && date.compare(interval[1]) <= 0
)
)}
/>
<Button type="submit">Submit</Button>
</Form>
);
}
API
<DatePicker>
<Label />
<Group>
<DateInput />
<Button />
</Group>
<Text slot="description" />
<FieldError />
<Popover>
<Calendar />
</Popover>
</DatePicker>
DatePicker
| Name | Type | Default |
|---|---|---|
pageBehavior | PageBehavior | Default: visible
|
Controls the behavior of paging. Pagination either works by advancing the visible page by visibleDuration (default) or one unit of visibleDuration. | ||
firstDayOfWeek | 'sun'
| 'mon'
| 'tue'
| 'wed'
| 'thu'
| 'fri'
| 'sat' | Default: — |
The day that starts the week. | ||
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 | ||
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. | ||
minValue | DateValue | null | Default: — |
The minimum allowed date that a user may select. | ||
maxValue | DateValue | null | Default: — |
The maximum allowed date that a user may select. | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the input value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
isOpen | boolean | Default: — |
Whether the overlay is open by default (controlled). | ||
defaultOpen | boolean | Default: — |
Whether the overlay is open by default (uncontrolled). | ||
onOpenChange | | Default: — |
Handler that is called when the overlay's open state changes. | ||
shouldCloseOnSelect | boolean | | Default: true
|
Determines whether the date picker popover should close automatically when a date is selected. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
style | StyleOrFunction | Default: — |
The inline style for the element. A function may be provided to compute the style based on component state. | ||
className | ClassNameOrFunction | Default: 'react-aria-DatePicker'
|
The CSS className for the element. A function may be provided to compute the class based on component state. | ||
name | string | Default: — |
The name of the input element, used when submitting an HTML form. See MDN. | ||
autoComplete | string | Default: — |
Describes the type of autocomplete functionality the input should provide if any. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
render | DOMRenderFunction | Default: — |
Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements:
| ||
Default className: react-aria-DatePicker
| Render Prop | CSS Selector |
|---|---|
isFocusWithin | CSS Selector: [data-focus-within]
|
| Whether an element within the date picker is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether an element within the date picker is keyboard focused. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the date picker is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the date picker is read only. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the date picker is invalid. | |
isRequired | CSS Selector: [data-required]
|
| Whether the date picker is required. | |
isOpen | CSS Selector: [data-open]
|
| Whether the date picker's popover is currently open. | |
state | CSS Selector: — |
| State of the date picker. | |