Time Input
The TimeInput component consists of a label, and a group of segments representing each unit of a time (e.g. hours, minutes, and seconds). Each segment is individually focusable and editable by the user, by typing or using the arrow keys to increment and decrement the value. This approach allows values to be formatted and parsed correctly regardless of the locale or time format, and offers an easy and error-free way to edit times using the keyboard.
Installation
npx nextui-cli@latest add date-input
The above command is for individual installation only. You may skip this step if @nextui-org/react is already installed globally.
Import
Usage
A TimeInput displays a placeholder by default. An initial, uncontrolled value can be provided to the TimeField using the defaultValue prop. Alternatively, a controlled value can be provided using the value prop.
Time values are provided using objects in the @internationalized/date package. This library handles correct international date and time manipulation across calendars, time zones, and other localization concerns.
TimeInput only supports selecting times, but values with date components are also accepted. By default, TimeInput will emit Time objects in the onChange event, but if a CalendarDateTime or ZonedDateTime object is passed as the value or defaultValue, values of that type will be emitted, changing only the time and preserving the date components.
Required
TimeInput supports the isRequired prop to ensure the user enters a value, as well as minimum and maximum values, and custom client and server-side validation.
Disabled
The isDisabled boolean prop makes TimeInput disabled. Inputs cannot be focused or selected.
Read Only
The isReadOnly boolean prop makes TimeInput's value immutable. Unlike isDisabled, TimeInput remains focusable.
Without Label
TimeInput supports the label prop to show or not show the label.
With Description
A description for the field. Provides a hint such as specific requirements for what to choose.
With Error Message
You can combine the isInvalid and errorMessage properties to show an invalid input.
You can also pass an error message as a function. This allows for dynamic error message handling based on the ValidationResult.
Label Placement
The label's overall position relative to the element it is labeling.
Start Content
If you want to display some content before the time inputs, you can set the startContent property.
End Content
If you want to display some content after the time inputs, you can set the endContent property.
Controlled
An initial, uncontrolled value can be provided to the TimeInput using the defaultValue prop. A controlled value can be provided using the value prop.
Time Zones
TimeInput is time zone aware when a ZonedDateTime object is provided as the value. In this case, the time zone abbreviation is displayed, and time zone concerns such as daylight saving time are taken into account when the value is manipulated.
In most cases, your data will come from and be sent to a server as an ISO 8601 formatted string. @internationalized/date includes functions for parsing strings in multiple formats into ZonedDateTime objects. Which format you use will depend on what information you need to store.
parseZonedDateTime– This function parses a date with an explicit time zone and optional UTC offset attached (e.g.2021-11-07T00:45[America/Los_Angeles]or2021-11-07T00:45-07:00[America/Los_Angeles]). This format preserves the maximum amount of information. If the exact local time and time zone that a user selected is important, use this format. Storing the time zone and offset that was selected rather than converting to UTC ensures that the local time is correct regardless of daylight saving rule changes (e.g. if a locale abolishes DST). Examples where this applies include calendar events, reminders, and other times that occur in a particular location.parseAbsolute– This function parses an absolute date and time that occurs at the same instant at all locations on Earth. It can be represented in UTC (e.g.2021-11-07T07:45:00Z), or stored with a particular offset (e.g.2021-11-07T07:45:00-07:00). A time zone identifier, e.g. America/Los_Angeles, must be passed, and the result will be converted into that time zone. Absolute times are the best way to represent events that occurred in the past, or future events where an exact time is needed, regardless of time zone.parseAbsoluteToLocal– This function parses an absolute date and time into the current user's local time zone. It is a shortcut for parseAbsolute, and accepts the same formats.
Granularity
The granularity prop allows you to control the smallest unit that is displayed by TimeInput. By default, times are displayed with "minute" granularity. More granular time values can be displayed by setting the granularity prop to "second".
Min Time Value
The minValue prop allows you to validate time value before a certain time.
Max Time Value
The maxValue prop allows you to validate time value before a certain time.
Placeholder Value
When no value is set, a placeholder is shown. The format of the placeholder is influenced by the granularity and placeholderValue props. placeholderValue also controls the default values of each segment when the user first interacts with them, e.g. using the up and down arrow keys. By default, the placeholderValue is midnight, but you can set it to a more appropriate value if needed.
Hide Time Zone
When a ZonedDateTime object is provided as the value to TimeInput, the time zone abbreviation is displayed by default. However, if this is displayed elsewhere or implicit based on the usecase, it can be hidden using the hideTimeZone option.
Hour Cycle
By default, TimeInput displays times in either 12 or 24 hour hour format depending on the user's locale. However, this can be overridden using the hourCycle prop if needed for a specific usecase. This example forces TimeInput to use 24-hour time, regardless of the locale.
Slots
- base: Input wrapper, it handles alignment, placement, and general appearance.
- label: Label of the time input, it is the one that is displayed above, inside or left of the time input.
- inputWrapper: Wraps the
label(when it is inside) and theinnerWrapper. - input: The time input element.
- innerWrapper: Wraps the segments, the
startContentand theendContent. - segment: The segment of input elements.
- helperWrapper: The wrapper of the helper text. This wraps the helper text and the error message.
- description: The description of the time input.
- errorMessage: The error message of the time input.
Data Attributes
TimeInput has the following attributes on the base element:
- data-has-helper:
When the time input has description or error message. Based on
descriptionorerrorMessageprops. - data-required:
When the time input is required. Based on
isRequiredprop. - data-disabled:
When the time input is disabled. Based on
isDisabledprop. - data-readonly:
When the time input is readonly. Based on
isReadOnlyprop. - data-invalid:
When the time input is invalid. Based on
isInvalidprop. - data-has-start-content:
When the time input has start content. Based on
startContentprop. - data-has-end-content:
When the time input has end content. Based on
endContentprop.
Accessibility
- Support for locale-specific formatting, number systems, hour cycles, and right-to-left layout.
- Times can optionally include a time zone. All modifications follow time zone rules such as daylight saving time.
- Each time unit is displayed as an individually focusable and editable segment, which allows users an easy way to edit times using the keyboard, in any format and locale.
- Time segments are editable using an easy to use numeric keypad, and all interactions are accessible using touch-based screen readers.
API
TimeInput Props
| Attribute | Type | Description | Default |
|---|---|---|---|
| label | ReactNode | The content to display as the label. | - |
| name | string | The name of the time input element, used when submitting an HTML form. See MDN. | - |
| value | TimeValue | null | The current value (controlled). | - |
| defaultValue | TimeValue | null | The default value (uncontrolled). | - |
| variant | flat | bordered | faded | underlined | The variant of the time input. | flat |
| color | default | primary | secondary | success | warning | danger | The color of the time input. | default |
| size | sm | md | lg | The size of the time input. | md |
| radius | none | sm | md | lg | full | The radius of the time input. | - |
| hourCycle | 12 | 24 | 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' | Determines the smallest unit that is displayed in the time picker. | minute |
| hideTimeZone | boolean | Whether to hide the time zone abbreviation. | - |
| labelPlacement | inside | outside | outside-left | The position of the label. | inside |
| shouldForceLeadingZeros | boolean | Whether to always show leading zeros in the hour field. By default, this is determined by the user's locale. | true |
| placeholderValue | TimeValue | 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. | - |
| minValue | TimeValue | The minimum allowed time that a user may select. | - |
| maxValue | TimeValue | The maximum allowed time that a user may select. | - |
| isDisabled | boolean | Whether the time input is disabled. | - |
| isReadOnly | boolean | Whether the time input can be selected but not changed by the user. | - |
| isRequired | boolean | Whether user time input is required on the time input before form submission. | - |
| isInvalid | boolean | Whether the time input is invalid. | - |
| autoFocus | boolean | Whether the element should receive focus on render. | - |
| description | ReactNode | A description for the field. Provides a hint such as specific requirements for what to choose. | - |
| errorMessage | ReactNode | (v: ValidationResult) => ReactNode | An error message for the field. | - |
| validate | (value: MappedTimeValue<TimeValue>) => ValidationError | true | null | undefined | Validate input values when committing (e.g. on blur), returning error messages for invalid values. Validation errors are displayed upon form submission if validationBehavior is set to native. For real-time validation, use the isInvalid prop. | - |
| validationBehavior | native | aria | 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. | aria |
| disableAnimation | boolean | Whether to disable the animation of the time input. | - |
| classNames | Record<"base"| "label"| "inputWrapper"| "innerWrapper" | "segment" | "helperWrapper" | "input" | "description" | "errorMessage", string> | Allows to set custom class names for the time input slots. | - |
TimeInput Events
| Attribute | Type | Description |
|---|---|---|
| onFocus | (e: FocusEvent<Target>) => void | Handler that is called when the element receives focus. |
| onBlur | (e: FocusEvent<Target>) => void | Handler that is called when the element loses focus. |
| onFocusChange | (isFocused: boolean) => void | Handler that is called when the element's focus status changes. |
| onKeyDown | (e: KeyboardEvent) => void | Handler that is called when a key is pressed. |
| onKeyUp | (e: KeyboardEvent) => void | Handler that is called when a key is released. |
| onChange | (value: MappedTimeValue<TimeValue>) => void | Handler that is called when the value changes. |

