--- title: "ion-datetime" --- import Props from '@ionic-internal/component-api/v8/datetime/props.md'; import Events from '@ionic-internal/component-api/v8/datetime/events.md'; import Methods from '@ionic-internal/component-api/v8/datetime/methods.md'; import Parts from '@ionic-internal/component-api/v8/datetime/parts.md'; import CustomProps from '@ionic-internal/component-api/v8/datetime/custom-props.mdx'; import Slots from '@ionic-internal/component-api/v8/datetime/slots.md'; import Basic from '@site/static/usage/v8/datetime/basic/index.md'; import MaxMin from '@site/static/usage/v8/datetime/date-constraints/max-min/index.md'; import Values from '@site/static/usage/v8/datetime/date-constraints/values/index.md'; import Advanced from '@site/static/usage/v8/datetime/date-constraints/advanced/index.md'; import FormatOptions from '@site/static/usage/v8/datetime/format-options/index.md'; import CustomLocale from '@site/static/usage/v8/datetime/localization/custom-locale/index.md'; import HourCycle from '@site/static/usage/v8/datetime/localization/hour-cycle/index.md'; import FirstDayOfWeek from '@site/static/usage/v8/datetime/localization/first-day-of-week/index.md'; import LocaleExtensionTags from '@site/static/usage/v8/datetime/localization/locale-extension-tags/index.md'; import TimeLabel from '@site/static/usage/v8/datetime/localization/time-label/index.md'; import MonthAndYear from '@site/static/usage/v8/datetime/presentation/month-and-year/index.md'; import Time from '@site/static/usage/v8/datetime/presentation/time/index.md'; import Date from '@site/static/usage/v8/datetime/presentation/date/index.md'; import ShowingDefaultTitle from '@site/static/usage/v8/datetime/title/showing-default-title/index.md'; import CustomizingTitle from '@site/static/usage/v8/datetime/title/customizing-title/index.md'; import ShowingConfirmationButtons from '@site/static/usage/v8/datetime/buttons/showing-confirmation-buttons/index.md'; import CustomizingButtons from '@site/static/usage/v8/datetime/buttons/customizing-buttons/index.md'; import CustomizingButtonTexts from '@site/static/usage/v8/datetime/buttons/customizing-button-texts/index.md'; import HighlightedDatesArray from '@site/static/usage/v8/datetime/highlightedDates/array/index.md'; import HighlightedDatesCallback from '@site/static/usage/v8/datetime/highlightedDates/callback/index.md'; import MultipleDateSelection from '@site/static/usage/v8/datetime/multiple/index.md'; import GlobalTheming from '@site/static/usage/v8/datetime/styling/global-theming/index.md'; import CalendarDaysStyling from '@site/static/usage/v8/datetime/styling/calendar-days/index.md'; import WheelStyling from '@site/static/usage/v8/datetime/styling/wheel-styling/index.md'; ion-datetime: Ionic API Input for Datetime Format Picker import EncapsulationPill from '@components/page/api/EncapsulationPill'; Datetimes present a calendar interface and time wheel, making it easy for users to select dates and times. Datetimes are similar to the native `input` elements of `datetime-local`, however, Ionic Framework's Datetime component makes it easy to display the date and time in the preferred format, and manage the datetime values. ## Overview Historically, handling datetime values within JavaScript, or even within HTML inputs, has always been a challenge. Specifically, JavaScript's `Date` object is notoriously difficult to correctly parse apart datetime strings or to format datetime values. Even worse is how different browsers and JavaScript versions parse various datetime strings differently, especially per locale. Fortunately, Ionic Framework's datetime input has been designed so developers can avoid the common pitfalls, allowing developers to easily manipulate datetime values and give the user a simple datetime picker for a great user experience. ### ISO 8601 Datetime Format: `YYYY-MM-DDTHH:mmZ` Ionic Framework uses the [ISO 8601 datetime format](https://www.w3.org/TR/NOTE-datetime) for its value. The value is simply a string, rather than using JavaScript's `Date` object. Using the ISO datetime format makes it easy to serialize and parse within JSON objects and databases. Below are some examples of ISO 8601 formats that can be used with `ion-datetime`: | Description | Format | Datetime Value Example | | -------------------- | ------------------------ | ------------------------------ | | Year | `YYYY` | `1994` | | Year and Month | `YYYY-MM` | `1994-12` | | Complete Date | `YYYY-MM-DD` | `1994-12-15` | | Date and Time | `YYYY-MM-DDTHH:mm` | `1994-12-15T13:47` | | UTC Timezone | `YYYY-MM-DDTHH:mm:ssZ` | `1994-12-15T13:47:20Z` | | Timezone Offset | `YYYY-MM-DDTHH:mm:ssTZD` | `1994-12-15T13:47:20+05:00` | | Hour and Minute | `HH:mm` | `13:47` | Note that the year is always four-digits, milliseconds (if it's added) is always three-digits, and all others are always two-digits. So the number representing January always has a leading zero, such as `01`. Additionally, the hour is always in the 24-hour format, so `00` is `12am` on a 12-hour clock, `13` means `1pm`, and `23` means `11pm`. :::note While seconds, milliseconds, and time zone can be specified using the ISO 8601 datetime format, `ion-datetime` does not provide an interface for second, millisecond, and time zone selection. Any second, millisecond, or time zone values provided will be ignored. ::: ## Basic Usage ## Usage with Datetime Button If you need to present a datetime in an overlay such as a modal or a popover, we recommend using [ion-datetime-button](./datetime-button). `ion-datetime-button` should be used when space is constrained. This component displays buttons which show the current date and time values. When the buttons are tapped, the date or time pickers open in the overlay. ## Setting Values Asynchronously If its `value` is updated programmatically after a datetime has already been created, the datetime will automatically jump to the new date. However, it is recommended to avoid updating the `value` in this way when users are able to interact with the datetime, as this could be disorienting for those currently trying to select a date. For example, if a datetime's `value` is loaded by an asynchronous process, it is recommended to hide the datetime with CSS until the value has finished updating. ## Date Constraints ### Max and Min Dates To customize the minimum and maximum datetime values, the `min` and `max` component properties can be provided which may make more sense for the app's use-case. Following the same IS0 8601 format listed in the table above, each component can restrict which dates can be selected by the user. The following example restricts date selection to March 2022 through May 2022 only. ### Selecting Specific Values While the `min` and `max` properties allow you to restrict date selection to a certain range, the `monthValues`, `dayValues`, `yearValues`, `hourValues`, and `minuteValues` properties allow you choose specific days and times that users can select. The following example allows minutes to be selected in increments of 15. It also allows for days to be selected in increments of 5. ### Advanced Date Constraints With the `isDateEnabled` property, developers can customize the `ion-datetime` to disable a specific day, range of dates, weekends or any custom rule using an ISO 8601 date string. The `isDateEnabled` property accepts a function returning a boolean, indicating if a date is enabled. The function is called for each rendered calendar day, for the previous, current and next month. Custom implementations should be optimized for performance to avoid jank. The following example shows how to disable all weekend dates. For more advanced date manipulation, we recommend using a date utility such as `date-fns`. ## Localization Ionic Framework makes use of the [Intl.DatetimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DatetimeFormat) Web API which allows us to automatically localize the month and day names according to the language and region set on the user's device. ### Custom Locale For instances where you need a specific locale, you can use the `locale` property to set it. The locale controls both the language and the date and time formats that are displayed. The following example shows how to set the locale to Spanish (Spain). :::note The time label is not automatically localized. See [Time Label](#time-label) for more information. ::: ### Hour Cycle `ion-datetime` will use the hour cycle that is specified by the `locale` property by default. For example, if `locale` is set to `en-US`, then `ion-datetime` will use a 12 hour cycle. There are 4 primary hour cycle types: | Hour cycle type | Description | | --------------- | ------------------------------------------------------------ | | `'h12'` | Hour system using 1–12; corresponds to 'h' in patterns. The 12 hour clock, with midnight starting at 12:00 am. | | `'h23'` | Hour system using 0–23; corresponds to 'H' in patterns. The 24 hour clock, with midnight starting at 0:00. | | `'h11'` | Hour system using 0–11; corresponds to 'K' in patterns. The 12 hour clock, with midnight starting at 0:00 am. | | `'h24'` | Hour system using 1–24; corresponds to 'k' in pattern. The 24 hour clock, with midnight starting at 24:00. | :::note Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle ::: There may be scenarios where you need to have more control over which hour cycle is used. This is where the `hourCycle` property can help. In the following example, we can use the `hourCycle` property to force `ion-datetime` to use the 12 hour cycle even though the locale is `en-GB`, which uses a 24 hour cycle by default: ### First Day of the Week For `ion-datetime`, the default first day of the week is Sunday. As of 2022, there is no browser API that lets Ionic automatically determine the first day of the week based on a device's locale, though there is on-going work regarding this (see: [TC39 GitHub](https://github.com/tc39/ecma402/issues/6)). ### Time Label The time label is not automatically localized. Fortunately, Ionic makes it easy to provide custom localizations with the `time-label` slot. ### Locale Extension Tags `ion-datetime` also supports [locale extension tags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale) as part of the `Intl.Locale` API. These tags let you encode information about the locale in the locale string itself. Developers may prefer to use the extension tag approach if they are using the [Intl.Locale API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale) in their apps. For example, if you wanted to use a 12 hour cycle with the `en-GB` locale, you could provide extension tags instead of using both the `locale` and `hourCycle` properties: :::note Be sure to check the [Browser Compatibility Chart](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale#browser_compatibility) for `Intl.Locale` before using it in your app. ::: ## Presentation By default, `ion-datetime` allows users to select both date and time. In addition, users have access to selecting the specific month, year, hour, and minute. Some use cases may call for only date selection or only time selection. The `presentation` property allows you to specify which pickers to show and the order to show them in. For example, setting `date-time` will have the calendar picker appear before the time picker. Setting `time-date` will have the calendar picker appear after the time picker. ### Month and Year Selection Month and year selection is available by passing `month-year`, `month`, or `year` to the `presentation` property. This example shows a datetime with the `month-year` configuration. ### Time Selection Time selection is available by passing `date-time`, `time-date`, or `time` to the `presentation` property. This example shows a datetime with the `time` configuration.