shoelace/docs/components/format-date.md

127 wiersze
4.0 KiB
Markdown
Czysty Zwykły widok Historia

2020-11-25 21:18:07 +00:00
# Format Date
[component-header:sl-format-date]
Formats a date/time using the specified locale and options.
Localization is handled by the browser's [`Intl.DateTimeFormat` API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat). No language packs are required.
```html preview
<!-- Shoelace 2 release date 🎉 -->
<sl-format-date date="2020-07-15T09:17:00-04:00"></sl-format-date>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlFormatDate } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlFormatDate date="2020-07-15T09:17:00-04:00" />;
2021-11-04 22:12:47 +00:00
```
2021-07-08 21:41:10 +00:00
The `date` attribute determines the date/time to use when formatting. It must be a string that [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) can interpret or a [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object set via JavaScript. If omitted, the current date/time will be assumed.
2020-11-25 21:18:07 +00:00
?> When using strings, avoid ambiguous dates such as `03/04/2020` which can be interpreted as March 4 or April 3 depending on the user's browser and locale. Instead, always use a valid [ISO 8601 date time string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#Date_Time_String_Format) to ensure the date will be parsed properly by all clients.
## Examples
### Date & Time Formatting
Formatting options are based on those found in the [`Intl.DateTimeFormat` API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat). When formatting options are provided, the date/time will be formatted according to those values. When no formatting options are provided, a localized, numeric date will be displayed instead.
```html preview
<!-- Human-readable date -->
2022-03-02 15:10:41 +00:00
<sl-format-date month="long" day="numeric" year="numeric"></sl-format-date><br />
2020-11-25 21:18:07 +00:00
<!-- Time -->
2022-03-02 15:10:41 +00:00
<sl-format-date hour="numeric" minute="numeric"></sl-format-date><br />
2020-11-25 21:18:07 +00:00
<!-- Weekday -->
2022-03-02 15:10:41 +00:00
<sl-format-date weekday="long"></sl-format-date><br />
2020-11-25 21:18:07 +00:00
<!-- Month -->
2022-03-02 15:10:41 +00:00
<sl-format-date month="long"></sl-format-date><br />
2020-11-25 21:18:07 +00:00
<!-- Year -->
2022-03-02 15:10:41 +00:00
<sl-format-date year="numeric"></sl-format-date><br />
2020-11-25 21:18:07 +00:00
<!-- No formatting options -->
<sl-format-date></sl-format-date>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlFormatDate } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
{/* Human-readable date */}
2022-03-02 15:10:41 +00:00
<SlFormatDate month="long" day="numeric" year="numeric" />
<br />
2021-11-04 22:12:47 +00:00
{/* Time */}
2022-03-02 15:10:41 +00:00
<SlFormatDate hour="numeric" minute="numeric" />
<br />
2021-11-04 22:12:47 +00:00
{/* Weekday */}
2022-03-02 15:10:41 +00:00
<SlFormatDate weekday="long" />
<br />
2021-11-04 22:12:47 +00:00
{/* Month */}
2022-03-02 15:10:41 +00:00
<SlFormatDate month="long" />
<br />
2021-11-04 22:12:47 +00:00
{/* Year */}
2022-03-02 15:10:41 +00:00
<SlFormatDate year="numeric" />
<br />
2021-11-04 22:12:47 +00:00
{/* No formatting options */}
<SlFormatDate />
</>
);
```
2020-11-25 21:18:07 +00:00
### Hour Formatting
2021-07-08 21:41:10 +00:00
By default, the browser will determine whether to use 12-hour or 24-hour time. To force one or the other, set the `hour-format` attribute to `12` or `24`.
2020-11-25 21:18:07 +00:00
```html preview
2022-03-02 15:10:41 +00:00
<sl-format-date hour="numeric" minute="numeric" hour-format="12"></sl-format-date><br />
2020-11-25 21:18:07 +00:00
<sl-format-date hour="numeric" minute="numeric" hour-format="24"></sl-format-date>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlFormatDate } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
<SlFormatDate hour="numeric" minute="numeric" hour-format="12" />
<br />
2021-11-04 22:12:47 +00:00
<SlFormatDate hour="numeric" minute="numeric" hour-format="24" />
</>
);
```
2020-11-25 21:18:07 +00:00
### Localization
2021-12-02 15:29:04 +00:00
Use the `lang` attribute to set the date/time formatting locale.
2020-11-25 21:18:07 +00:00
```html preview
2022-03-02 15:10:41 +00:00
English: <sl-format-date lang="en"></sl-format-date><br />
French: <sl-format-date lang="fr"></sl-format-date><br />
2021-12-02 15:29:04 +00:00
Russian: <sl-format-date lang="ru"></sl-format-date>
2021-11-04 22:12:47 +00:00
```
```jsx react
import { SlFormatDate } from '@shoelace-style/shoelace/dist/react';
const App = () => (
<>
2022-03-02 15:10:41 +00:00
English: <SlFormatDate lang="en" />
<br />
French: <SlFormatDate lang="fr" />
<br />
2021-12-02 15:29:04 +00:00
Russian: <SlFormatDate lang="ru" />
2021-11-04 22:12:47 +00:00
</>
);
2020-11-25 21:18:07 +00:00
```
[component-metadata:sl-format-date]