-
Notifications
You must be signed in to change notification settings - Fork 3.2k
docs(datetime, datetime-button): document formatOptions property #3452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f74ec55
9ab2baa
0d40670
3458632
100cec6
7a8b2ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ import MaxMin from '@site/static/usage/v7/datetime/date-constraints/max-min/inde | |
import Values from '@site/static/usage/v7/datetime/date-constraints/values/index.md'; | ||
import Advanced from '@site/static/usage/v7/datetime/date-constraints/advanced/index.md'; | ||
|
||
import FormatOptions from '@site/static/usage/v7/datetime/format-options/index.md'; | ||
|
||
import CustomLocale from '@site/static/usage/v7/datetime/localization/custom-locale/index.md'; | ||
import HourCycle from '@site/static/usage/v7/datetime/localization/hour-cycle/index.md'; | ||
import FirstDayOfWeek from '@site/static/usage/v7/datetime/localization/first-day-of-week/index.md'; | ||
|
@@ -268,6 +270,16 @@ By default, `ion-datetime` does not show any header or title associated with the | |
|
||
<CustomizingTitle /> | ||
|
||
## Format Options | ||
|
||
You can customize the format of the date in the header text and the time in the time button of a Datetime component by providing `formatOptions`. The `date` and `time` in the `formatObjects` property should each be an [`Intl.DateTimeFormatOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options) object. If `formatObjects` is not provided, default formats will be used for dates and times. | ||
mapsandapps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
If `timeZone` or `timeZoneName` are provided, they will be ignored, and the time zone will be set to UTC. This ensures that the displayed value matches the selected value, rather than being converted to the user's current time zone. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we link to the timezone section in this doc page that talks about how datetime intentionally does not handle timezones? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea! added in 3458632 |
||
|
||
Be careful with the options you provide, as they may not match the selected presentation. For example, providing `minute: 'numeric'` for a presentation of `month` may lead to unexpected behavior, displaying a month where only a time might be expected. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: I could be totally wrong, but I feel like most of the time using either |
||
|
||
<FormatOptions /> | ||
|
||
## Buttons | ||
|
||
By default, `ionChange` is emitted with the new datetime value whenever a new date is selected. To require user confirmation before emitting `ionChange`, you can either set the `showDefaultButtons` property to `true` or use the `buttons` slot to pass in a custom confirmation button. When passing in custom buttons, the confirm button must call the `confirm` method on `ion-datetime` for `ionChange` to be emitted. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
```html | ||
<ion-datetime-button datetime="datetime"></ion-datetime-button> | ||
|
||
<ion-modal [keepContentsMounted]="true"> | ||
<ng-template> | ||
<ion-datetime | ||
id="datetime" | ||
presentation="date-time" | ||
value="2023-11-02T01:22:00" | ||
[formatOptions]="{ | ||
date: { | ||
weekday: 'short', | ||
month: 'long', | ||
day: '2-digit', | ||
}, | ||
time: { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}, | ||
}" | ||
></ion-datetime> | ||
</ng-template> | ||
</ion-modal> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Datetime Button</title> | ||
<link rel="stylesheet" href="../../../common.css" /> | ||
<script src="../../../common.js"></script> | ||
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/ionic.esm.js"></script> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@7/css/ionic.bundle.css" /> | ||
</head> | ||
|
||
<body> | ||
<ion-app> | ||
<ion-content> | ||
<div class="container"> | ||
<ion-datetime-button datetime="datetime"></ion-datetime-button> | ||
|
||
<ion-modal keep-contents-mounted="{true}"> | ||
<ion-datetime id="datetime" presentation="date-time" value="2023-11-02T01:22:00"></ion-datetime> | ||
</ion-modal> | ||
</div> | ||
</ion-content> | ||
</ion-app> | ||
|
||
<script> | ||
const datetime = document.querySelector('ion-datetime'); | ||
datetime.formatOptions = { | ||
date: { | ||
weekday: 'short', | ||
month: 'long', | ||
day: '2-digit', | ||
}, | ||
time: { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}, | ||
}; | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Playground from '@site/src/components/global/Playground'; | ||
|
||
import javascript from './javascript.md'; | ||
import react from './react.md'; | ||
import vue from './vue.md'; | ||
import angular from './angular.md'; | ||
|
||
<Playground | ||
version="7" | ||
code={{ | ||
mapsandapps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
javascript, | ||
react, | ||
vue, | ||
angular, | ||
}} | ||
src="usage/v7/datetime-button/format-options/demo.html" | ||
/> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
```html | ||
<ion-datetime-button datetime="datetime"></ion-datetime-button> | ||
|
||
<ion-modal keep-contents-mounted="{true}"> | ||
mapsandapps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<ion-datetime id="datetime" presentation="date-time" value="2023-11-02T01:22:00"></ion-datetime> | ||
</ion-modal> | ||
|
||
<script> | ||
const datetime = document.querySelector('ion-datetime'); | ||
datetime.formatOptions = { | ||
date: { | ||
weekday: 'short', | ||
month: 'long', | ||
day: '2-digit', | ||
}, | ||
time: { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}, | ||
}; | ||
</script> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
```tsx | ||
import React from 'react'; | ||
import { IonDatetime, IonDatetimeButton, IonModal } from '@ionic/react'; | ||
|
||
function Example() { | ||
return ( | ||
<> | ||
<IonDatetimeButton datetime="datetime"></IonDatetimeButton> | ||
|
||
<IonModal keepContentsMounted={true}> | ||
<IonDatetime | ||
id="datetime" | ||
presentation="date-time" | ||
value="2023-11-02T01:22:00" | ||
formatOptions={{ | ||
date: { | ||
weekday: 'short', | ||
month: 'long', | ||
day: '2-digit', | ||
}, | ||
time: { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}, | ||
}} | ||
></IonDatetime> | ||
</IonModal> | ||
</> | ||
); | ||
} | ||
export default Example; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
```html | ||
<template> | ||
<ion-datetime-button datetime="datetime"></ion-datetime-button> | ||
|
||
<ion-modal :keep-contents-mounted="true"> | ||
<ion-datetime | ||
id="datetime" | ||
presentation="date-time" | ||
value="2023-11-02T01:22:00" | ||
:format-options="formatOptions" | ||
></ion-datetime> | ||
</ion-modal> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { IonDatetime, IonDatetimeButton, IonModal } from '@ionic/vue'; | ||
import { defineComponent } from 'vue'; | ||
|
||
const formatOptions = { | ||
date: { | ||
weekday: 'short', | ||
month: 'long', | ||
day: '2-digit', | ||
}, | ||
time: { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}, | ||
}; | ||
</script> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
```html | ||
<ion-datetime | ||
value="2023-11-02T01:22:00" | ||
[formatOptions]="{ | ||
time: { hour: '2-digit', minute: '2-digit' }, | ||
date: { day: '2-digit', month: 'long' }, | ||
}" | ||
> | ||
<span slot="title">Select Date</span> | ||
</ion-datetime> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Datetime</title> | ||
<link rel="stylesheet" href="../../../common.css" /> | ||
<script src="../../../common.js"></script> | ||
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/ionic.esm.js"></script> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@7/css/ionic.bundle.css" /> | ||
<style> | ||
ion-datetime { | ||
width: 350px; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<ion-app> | ||
<ion-content> | ||
<div class="container"> | ||
<ion-datetime value="2023-11-02T01:22:00"> | ||
<span slot="title">Select Date</span> | ||
</ion-datetime> | ||
</div> | ||
</ion-content> | ||
</ion-app> | ||
|
||
<script> | ||
const datetime = document.querySelector('ion-datetime'); | ||
datetime.formatOptions = { | ||
time: { hour: '2-digit', minute: '2-digit' }, | ||
date: { day: '2-digit', month: 'long' }, | ||
}; | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Playground from '@site/src/components/global/Playground'; | ||
|
||
import javascript from './javascript.md'; | ||
import react from './react.md'; | ||
import vue from './vue.md'; | ||
import angular from './angular.md'; | ||
|
||
<Playground | ||
version="7" | ||
size="large" | ||
code={{ | ||
javascript, | ||
react, | ||
vue, | ||
angular, | ||
}} | ||
src="usage/v7/datetime/format-options/demo.html" | ||
/> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```html | ||
<ion-datetime value="2023-11-02T01:22:00"> | ||
<span slot="title">Select Date</span> | ||
</ion-datetime> | ||
|
||
<script> | ||
const datetime = document.querySelector('ion-datetime'); | ||
datetime.formatOptions = { | ||
time: { hour: '2-digit', minute: '2-digit' }, | ||
date: { day: '2-digit', month: 'long' }, | ||
}; | ||
</script> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
```tsx | ||
import React from 'react'; | ||
import { IonDatetime } from '@ionic/react'; | ||
|
||
function Example() { | ||
return ( | ||
<IonDatetime | ||
value="2023-11-02T01:22:00" | ||
formatOptions={{ | ||
time: { hour: '2-digit', minute: '2-digit' }, | ||
date: { day: '2-digit', month: 'long' }, | ||
}} | ||
> | ||
<span slot="title">Select Date</span> | ||
</IonDatetime> | ||
); | ||
} | ||
export default Example; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
```html | ||
<template> | ||
<ion-datetime value="2023-11-02T01:22:00" :format-options="formatOptions"> | ||
<span slot="title">Select Date</span> | ||
</ion-datetime> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { IonDatetime } from '@ionic/vue'; | ||
import { defineComponent } from 'vue'; | ||
|
||
const formatOptions = { | ||
time: { hour: '2-digit', minute: '2-digit' }, | ||
date: { day: '2-digit', month: 'long' }, | ||
}; | ||
</script> | ||
``` |
Uh oh!
There was an error while loading. Please reload this page.