A time field allows users to enter and edit time values using a keyboard. Each part of a time value is displayed in an individually editable segment.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.Value
Use the value or defaultValue prop to set the time value, using objects in the @internationalized/date package. TimeField accepts plain Time, CalendarDateTime, or ZonedDateTime, but only displays the time.
Selected time: 11:45:00
import {Time} from '@internationalized/date';
import {TimeField} from './TimeField';
import {useState} from 'react';
function Example() {
let [time, setTime] = useState<Time | null>(new Time(11, 45));
return (
<>
<TimeField
value={time}
onChange={setTime} />
<p>Selected time: {time ? time.toString() : '--'}</p>
</>
);
}
Format options
The time format is automatically determined based on the user's locale. TimeField supports several props to control how values are displayed.
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 {Time} from '@internationalized/date';
import {TimeField} from './TimeField';
import {Button} from './Button';
import {Form} from './Form';;
<Form>
<TimeField
label="Appointment time"
name="time"
isRequired
minValue={new Time(9)}
maxValue={new Time(17)}
validate={time => time?.minute % 15 !== 0 ? 'Appointments start every 15 minutes.' : null}
/>
<Button type="submit">Submit</Button>
</Form>
API
<TimeField>
<Label />
<DateInput>
{segment => <DateSegment segment={segment} />}
</DateInput>
<Text slot="description" />
<FieldError />
</TimeField>
TimeField
A time field allows users to enter and edit time values using a keyboard. Each part of a time value is displayed in an individually editable segment.
| Name | Type | Default |
|---|---|---|
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 | 'hour'
| 'minute'
| 'second' | Default: 'minute'
|
| Determines the smallest unit that is displayed in the time picker. | ||
hideTimeZone | boolean | Default: — |
| Whether to hide the time zone abbreviation. | ||
shouldForceLeadingZeros | boolean | Default: — |
| Whether to always show leading zeros in the hour field. By default, this is determined by the user's locale. | ||
placeholderValue | TimeValue | Default: — |
| A placeholder time that influences the format of the placeholder shown when no value is selected. Defaults to 12:00 AM or 00:00 depending on the hour cycle. | ||
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 | TimeValue | null | Default: — |
| The current value (controlled). | ||
defaultValue | TimeValue | null | Default: — |
| The default value (uncontrolled). | ||
onChange | | Default: — |
| Handler that is called when the value changes. | ||
Default className: react-aria-TimeField
| 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. | |