packages/dev/s2-docs/pages/s2/DateField.mdx
import {Layout} from '../../src/Layout'; export default Layout;
import {DateField} from '@react-spectrum/s2'; import docs from 'docs:@react-spectrum/s2';
export const tags = ['calendar']; export const description = 'Allows a user to enter and edit date values using a keyboard.';
<PageDescription>{docs.exports.DateField.description}</PageDescription>
<VisualExample component={DateField} docs={docs.exports.DateField} props={['label', 'granularity', 'size', 'labelPosition', 'description', 'contextualHelp', 'isDisabled']} initialProps={{label: 'Birth date'}} importSource="@react-spectrum/s2" type="s2" />
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, CalendarDate} from '@internationalized/date';
import {useDateFormatter} from 'react-aria';
import {DateField} from '@react-spectrum/s2';
import {useState} from 'react';
function Example() {
let [date, setDate] = useState<CalendarDate | null>(parseDate('2020-02-03'));
let formatter = useDateFormatter({ dateStyle: 'full' });
return (
<>
<DateField
///- begin highlight -///
value={date}
onChange={setDate} />
<p>Selected date: {date ? formatter.format(date.toDate(getLocalTimeZone())) : '--'}</p>
</>
);
}
The date format is automatically determined based on the user's locale. DateField supports several props to control how values are displayed.
"use client";
import {parseZonedDateTime} from '@internationalized/date';
import {DateField} from '@react-spectrum/s2';
<DateField
/* PROPS */
defaultValue={parseZonedDateTime('2025-02-03T08:45:00[America/Los_Angeles]')} />
By default, DateField displays the value using the calendar system for the user's locale. Use <Provider> 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 {Provider, DateField} from '@react-spectrum/s2';
import {parseZonedDateTime} from '@internationalized/date';
<Provider/* PROPS */>
<DateField defaultValue={parseZonedDateTime('2025-02-03T08:45:00[America/Los_Angeles]')} />
</Provider>
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.
"use client";
import {today, getLocalTimeZone} from '@internationalized/date';
import {DateField, Form, Button} from '@react-spectrum/s2';
function Example(props) {
return (
<Form>
<DateField
{...props}
label="Appointment date"
///- begin highlight -///
name="date"
/* PROPS */
minValue={today(getLocalTimeZone())}
///- end highlight -///
/>
<Button type="submit">Submit</Button>
</Form>
);
}