Enables users to input specific dates within a designated field.
Birthday
mm
/
dd
/
yyyy
Overview
The DateField component is an alternative to the native <input type="date"> element. It provides a more flexible and customizable way to select dates within a designated field.
Before jumping into the DateField component, it's important to understand how dates and times are handled in Bits UI. You can learn more about this on the Dates page.
Structure
Reusable Components
It's recommended to use the DateField primitives to build your own custom date field component that can be used throughout your application.
The following example shows how you might create a reusable MyDateField component that can be used throughout your application. For style inspiration, reference the featured demo at the top of this page.
Select a date
mm
/
dd
/
yyyy
We'll be using this newly created MyDateField component in the following demos and examples to prevent repeating the same code, so be sure to reference it as you go through the documentation.
Segments
A segment of the DateField represents a not only a specific part of the date, such as the day, month, year, hour, but can also reference a "literal" which is typically a separator between the different parts of the date, and varies based on the locale.
Notice that in the MyDateField component we created, we're styling the DateField.Segment components differently based on whether they are a "literal" or not.
Placeholder
The placeholder prop for the DateField.Root component isn't what is displayed when the field is empty, but rather what date our field should start with when the user attempts to cycle through the segments. The placeholder can also be used to set a granularity for the date field, which will determine which type of DateValue object is used for the value.
By default, the placeholder will be set to the current date, and be of type CalendarDate. However, if we wanted this date field to also allow for selecting a time, we could set the placeholder to a CalendarDateTime object.
Select a date
mm
/
dd
/
yyyy
,
––
:
––
PM
If we're collecting a date from the user where we want the timezone as well, we can use a ZonedDateTime object instead.
Select a date
mm
/
dd
/
yyyy
,
––
:
––
PM
EST
NOTE: If you're creating a date field for something like a birthday, ensure your placeholder is set in a leap year to ensure users born on a leap year will be able to select the correct date.
Managing Placeholder State
Bits UI provides flexible options for controlling and synchronizing the DateField.Root component's placeholder state.
Two-Way Binding
Use the bind:placeholder directive for effortless two-way synchronization between your local state and the DateField.Root component's placeholder.
This setup enables toggling the DateField.Root component's placeholder via the custom button and ensures the local placeholder state is synchronized with the DateField.Root component's placeholder.
Change Handler
You can also use the onPlaceholderChange prop to update local state when the DateField.Root component's placeholder changes. This is useful when you don't want two-way binding for one reason or another, or you want to perform additional logic when the DateField.Root component's placeholder changes.
Managing Value State
The value represents the currently selected date within the DateField.Root component.
Bits UI provides flexible options for controlling and synchronizing the DateField.Root component's value state.
Two-Way Binding
Use the bind:value directive for effortless two-way synchronization between your local state and the DateField.Root component's value.
This setup enables toggling the DateField.Root component's value via the custom button and ensures the local value state is synchronized with the DateField.Root component's value.
Change Handler
You can also use the onValueChange prop to update local state when the DateField.Root component's value changes. This is useful when you don't want two-way binding for one reason or another, or you want to perform additional logic when the DateField.Root component's value changes.
Default Value
Often, you'll want to start the DateField.Root component with a default value. Likely this value will come from a database in the format of an ISO 8601 string. You can use the parseDate function from the @internationalized/date package to parse the string into a CalendarDate object.
Select a date
08
/
03
/
2024
Now our input is populated with the default value. In addition to the parseDate function, you can also use parseDateTime or parseZonedDateTime to parse the string into a CalendarDateTime or ZonedDateTime object respectively.
Validation
Minimum Value
You can set a minimum value for the DateField.Root component by using the minValue prop. If a user selects a date that is less than the minimum value, the date field will be marked as invalid.
Select a date
11
/
30
/
2024
In the example above, we're setting the minimum value to today, and the default value to yesterday. This causes the date field to be marked as invalid, and we can style it accordingly. If you adjust the date to be greater than the minimum value, the invalid state will be cleared.
Maximum Value
You can set a maximum value for the DateField.Root component by using the maxValue prop. If a user selects a date that is greater than the maximum value, the date field will be marked as invalid.
Select a date
12
/
02
/
2024
In the example above, we're setting the maximum value to today, and the default value to tomorrow. This causes the date field to be marked as invalid, and we can style it accordingly. If you adjust the date to be less than the maximum value, the invalid state will be cleared.
Unavailable Dates
You can specify speciifc dates that are unavailable for selection by using the isDateUnavailable prop. This prop accepts a function that returns a boolean value indicating whether a date is unavailable or not.
Select a date
08
/
02
/
2024
In the example above, we're setting the isDateUnavailable prop to a function that returns true for the first day of the month. Try selecting a date that is the first day of the month to see the date field marked as invalid.
Granularity
The granularity prop sets the granularity of the date field, which determines which segments are rendered in the date field. The granularity can be set to either 'day', 'hour', 'minute', or 'second'.
Select a date
08
/
02
/
2024
,
12
:
30
:
00
PM
In the example above, we're setting the granularity to 'second', which means that the date field will include an additional segment for the seconds.
API Reference
DateField.Root
The root date field component.
Property
Type
Description
valuebindable prop
DateValue
The selected date.
Default: ——undefined
onValueChange
function
A function that is called when the selected date changes.
Default: ——undefined
placeholderbindable prop
DateValue
The placeholder date, which is used to determine what date to start the segments from when no value exists.
Default: ——undefined
onPlaceholderChange
function
A function that is called when the placeholder date changes.
Default: ——undefined
required
boolean
Whether or not the date field is required.
Default: false
isDateUnavailable
function
A function that returns whether or not a date is unavailable.
Default: ——undefined
hourCycle
enum
The hour cycle to use for formatting times. Defaults to the locale preference
Default: ——undefined
granularity
enum
The granularity to use for formatting the field. Defaults to 'day' if a CalendarDate is provided, otherwise defaults to 'minute'. The field will render segments for each part of the date up to and including the specified granularity.
Default: ——undefined
hideTimeZone
boolean
Whether or not to hide the time zone segment of the field.
Default: false
maxValue
DateValue
The maximum valid date that can be entered.
Default: ——undefined
minValue
DateValue
The minimum valid date that can be entered.
Default: ——undefined
locale
string
The locale to use for formatting dates.
Default: 'en-US'
disabled
boolean
Whether or not the accordion is disabled.
Default: false
readonly
boolean
Whether or not the field is readonly.
Default: false
readonlySegments
array
An array of segments that should be readonly, which prevent user input on them.
Default: ——undefined
children
Snippet
The children content to render.
Default: ——undefined
DateField.Input
The container for the segments of the date field.
Property
Type
Description
name
string
The name of the date field used for form submission. If provided, a hidden input element will be rendered alongside the date field.
Default: ——undefined
refbindable prop
HTMLDivElement
The underlying DOM element being rendered. You can bind to this to get a reference to the element.
Default: ——undefined
children
Snippet
The children content to render.
Default: ——undefined
child
Snippet
Use render delegation to render your own element. See delegation docs for more information.
Default: ——undefined
Data Attribute
Value
Description
data-invalid
''
Present on the element when the field is invalid.
data-disabled
''
Present on the element when the field is disabled.
data-date-field-input
''
Present on the element.
DateField.Segment
A segment of the date field.
Property
Type
Description
partrequired prop
SegmentPart
The part of the date to render.
Default: ——undefined
refbindable prop
HTMLDivElement
The underlying DOM element being rendered. You can bind to this to get a reference to the element.
Default: ——undefined
children
Snippet
The children content to render.
Default: ——undefined
child
Snippet
Use render delegation to render your own element. See delegation docs for more information.
Default: ——undefined
Data Attribute
Value
Description
data-invalid
''
Present on the element when the field is invalid
data-disabled
''
Present on the element when the field is disabled
data-readonly
''
Present on the element when the field or segment is readonly
data-segment
enum
The part of the date to render.
data-date-field-segment
''
Present on the element.
DateField.Label
The label for the date field.
Property
Type
Description
refbindable prop
HTMLSpanElement
The underlying DOM element being rendered. You can bind to this to get a reference to the element.
Default: ——undefined
children
Snippet
The children content to render.
Default: ——undefined
child
Snippet
Use render delegation to render your own element. See delegation docs for more information.