import {Layout} from '../../src/Layout'; export default Layout;
import docs from 'docs:react-aria-components'; import vanillaDocs from 'docs:vanilla-starter/DatePicker'; import {DatePicker as VanillaDatePicker} from 'vanilla-starter/DatePicker'; import {DatePicker as TailwindDatePicker} from 'tailwind-starter/DatePicker'; import '../../tailwind/tailwind.css'; import Anatomy from '@react-aria/datepicker/docs/datepicker-anatomy.svg';
export const tags = ['calendar', 'input'];
{docs.exports.DatePicker.description}
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.
"use client";
import {parseDate, getLocalTimeZone} from '@internationalized/date';
import {useDateFormatter} from 'react-aria';
import {DatePicker} from 'vanilla-starter/DatePicker';
import {useState} from 'react';
function Example() {
let [date, setDate] = useState(parseDate('2020-02-03'));
let formatter = useDateFormatter({ dateStyle: 'full' });
return (
<>
<DatePicker
///- begin highlight -///
value={date}
onChange={setDate} />
{/*- end highlight -*/}
<p>Selected date: {date ? formatter.format(date.toDate(getLocalTimeZone())) : '--'}</p>
</>
);
}The date format is automatically determined based on the user's locale. DatePicker supports several props to control how values are displayed.
"use client";
import {parseZonedDateTime} from '@internationalized/date';
import {DatePicker} from 'vanilla-starter/DatePicker';
<DatePicker
/* PROPS */
defaultValue={parseZonedDateTime('2025-02-03T08:45:00[America/Los_Angeles]')} />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.
"use client";
import {I18nProvider} from 'react-aria-components';
import {parseZonedDateTime} from '@internationalized/date';
import {DatePicker} from 'vanilla-starter/DatePicker';
<I18nProvider/* PROPS */>
<DatePicker defaultValue={parseZonedDateTime('2025-02-03T08:45:00[America/Los_Angeles]')} />
</I18nProvider>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.
"use client";
import {isWeekend, today, getLocalTimeZone} from '@internationalized/date';
import {useLocale} from 'react-aria';
import {DatePicker} from 'vanilla-starter/DatePicker';
import {Button} from 'vanilla-starter/Button';
import {Form} from 'react-aria-components';
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"
///- begin highlight -///
name="date"
isRequired
minValue={today(getLocalTimeZone())}
isDateUnavailable={date => (
isWeekend(date, locale) ||
disabledRanges.some((interval) =>
date.compare(interval[0]) >= 0 && date.compare(interval[1]) <= 0
)
)}
///- end highlight -///
/>
<Button type="submit">Submit</Button>
</Form>
);
}<DatePicker>
<Label />
<Group>
<DateInput />
<Button />
</Group>
<Text slot="description" />
<FieldError />
<Popover>
<Dialog>
<Calendar />
</Dialog>
</Popover>
</DatePicker>