Dates and Times
How to work with the various date and time components in Bits UI.
The date and time components in Bits UI are built on top of the @internationalized/date
package, which provides a unified API for working with dates and times in different locales and time zones. It's heavily inspired by the Temporal proposal, and intends to back the objects in this package with the Temporal API once it's available.
You can install the package using your favorite package manager:
It's highly recommended to familiarize yourself with the package's documentation before diving into the components. We'll cover the basics of how we use the package in Bits UI in the sections below, but their documentation provides much more detail on the various formats and how to work with them.
DateValue
We use the DateValue
objects provided by @internationalized/date
to represent dates and times in a consistent way. These objects are immutable and provide information about the type of date they represent. The DateValue
is a union of the following three types:
CalendarDate
- Represents a date with no time component, such as2024-07-10
CalendarDateTime
- Represents a date and time, such as2024-07-10T12:30:00
ZonedDateTime
- Represents a date and time with a time zone, such as2023-10-11T21:00:00:00-04:00[America/New_York]
The benefit of using these objects is that they allow you to be very specific about the type of date you want, and the component will adapt to that type. For example, if you pass a CalendarDate
object to a DateField
component, it will only display the date portion of the date, without the time. See the Date Field component for more information.
CalendarDate
The CalendarDate
object represents a date with no time component, such as 2024-07-10
.
You can use the CalendarDate
constructor to create a new CalendarDate
object:
You can also use the parseDate
function to parse an ISO 8601 string into a CalendarDate
object:
If you want to create a CalendarDate
with the current date, you can use the today
function. This function requires a timezone identifier as an argument, which can be passed in as a string, or by using getLocalTimeZone
which returns the user's current time zone:
See the CalendarDate API documentation for more information.
CalendarDateTime
The CalendarDateTime
object represents a date and time, such as 2024-07-10T12:30:00
.
You can use the CalendarDateTime
constructor to create a new CalendarDateTime
object:
You can also use the parseDateTime
function to parse an ISO 8601 string into a CalendarDateTime
object:
See the CalendarDateTime API documentation for more information.
ZonedDateTime
The ZonedDateTime
object represents a date and time with a time zone, which represents an exact date and time in a specific time zone. ZonedDateTimes
are often used for things such as in person events (concerts, conferences, etc.), where you want to represent a date and time in a specific time zone, rather than a specific date and time in the user's local time zone.
You can use the ZonedDateTime
constructor to create a new ZonedDateTime
object:
You can also use one of the following parsing functions to parse an ISO 8601 string into a ZonedDateTime
object:
See the ZonedDateTime API documentation for more information.
Date Ranges
Bits UI also provides a DateRange
type with the following structure:
This type is used to represent the value of the various date range components in Bits UI, such as the Date Range Field, Date Range Picker, and Range Calendar.
Placeholder
Each of the date/time components in Bits UI has a bindable placeholder
prop, which acts as the starting point for the component when no value is present. The placeholder value is used to determine the type of date/time to display, and the component and its value will adapt to that type.
For example, if you pass a CalendarDate
object to a DateField
component, it will only display the date portion of the date, without the time. If you pass a CalendarDateTime
object, it will display the date and time. If you pass a ZonedDateTime
object, it will display the date and time with the time zone information.
In addition to setting the starting point and type of the date/time, the placeholder is also used to control the view of the calendar. For example, if you wanted to give the user the ability to select a specific month to jump to in the calendar, you could simply update the placeholder to a DateValue
representing that month. Here's an example of how you might do that:
In the example above, we're using the placeholder
value to control the view of the calendar. The user can select a specific month to jump to in the calendar, and the placeholder will be updated to reflect the selected month. When the placeholder is updated, the calendar view will automatically update to reflect that new month.
As the user interacts with the calendar, the placeholder will be updated to reflect the currently focused date in the calendar. If a value is selected in the calendar, the placeholder will be updated to reflect that selected value.
Updating the placeholder
It's important to note that DateValue
objects are immutable, so you can't directly update the placeholder
value. Instead, you'll need to reassign the value to the placeholder
prop for the changes to reflect.
@internationalized/date
provides a number of methods for updating the DateValue
objects, such as set
, add
, subtract
, and cycle
, each of which will return a new DateValue
object with the updated value.
For example, if you wanted to update the placeholder to the next month, you could use the add
method to add one month to the current month in the placeholder
value:
Formatting Dates
@internationalized/date
provides a DateFormatter
class that is a wrapper around the Intl.DateTimeFormat
API that fixes various browser bugs, and polyfills new features.
It's highly recommended to use this class to format dates and times in your application, as it will ensure that the formatting is accurate for all locales, time zones, and calendars.
Parsing Dates
Often, you'll want to parse a string from a database or other source into a DateValue
object for use with the date/time components in Bits UI. @internationalized/date
provides various parsing functions that can be used to parse strings into each of the supported DateValue
objects.
parseDate
The parseDate
function is used to parse a string into a CalendarDate
object.