포맷 데이트 (Format Date)
지정한 로케일과 옵션에 따라 날짜/시간을 포맷해 주는 컴포넌트예요. 지역화(localization)는 브라우저의 Intl.DateTimeFormat API가 처리하므로 별도의 언어 팩이 필요 없어요. _ | SlFormatDate, Shoelace 2.0부터 제공되고 있으며 stable 상태예요.
출처: 문서
본문
| SlFormatDate — Since 2.0, stable
지정한 로케일과 옵션에 따라 날짜/시간을 포맷해 줘요.
지역화(localization)는 브라우저의 Intl.DateTimeFormat API가 처리해요. 언어 팩이 필요 없답니다.
<!-- Shoelace 2 release date 🎉 -->
<sl-format-date date="2020-07-15T09:17:00-04:00"></sl-format-date>
import SlFormatDate from '@shoelace-style/shoelace/dist/react/format-date';
const App = () => <SlFormatDate date="2020-07-15T09:17:00-04:00" />;
date 속성은 포맷할 때 사용할 날짜/시간을 결정해요. 반드시 Date.parse()가 해석할 수 있는 문자열이거나, JavaScript로 설정한 Date 객체여야 해요. 생략하면 현재 날짜/시간이 사용돼요.
문자열을 쓸 때는 03/04/2020 같은 모호한 날짜는 피하세요. 이런 형식은 사용자의 브라우저와 로케일에 따라 3월 4일 또는 4월 3일로 해석될 수 있거든요. 대신 항상 유효한 ISO 8601 날짜-시간 문자열을 사용해서 모든 클라이언트가 날짜를 올바르게 파싱하도록 하세요.
Examples
Date & Time Formatting
포맷 옵션은 Intl.DateTimeFormat API에서 제공하는 것을 기반으로 해요. 포맷 옵션을 주면 그 값대로 날짜/시간이 포맷되고, 옵션을 주지 않으면 지역화된 숫자 형식의 날짜가 표시돼요.
<!-- Human-readable date -->
<sl-format-date month="long" day="numeric" year="numeric"></sl-format-date><br />
<!-- Time -->
<sl-format-date hour="numeric" minute="numeric"></sl-format-date><br />
<!-- Weekday -->
<sl-format-date weekday="long"></sl-format-date><br />
<!-- Month -->
<sl-format-date month="long"></sl-format-date><br />
<!-- Year -->
<sl-format-date year="numeric"></sl-format-date><br />
<!-- No formatting options -->
<sl-format-date></sl-format-date>
import SlFormatDate from '@shoelace-style/shoelace/dist/react/format-date';
const App = () => (
<>
{/* Human-readable date */}
<SlFormatDate month="long" day="numeric" year="numeric" />
<br />
{/* Time */}
<SlFormatDate hour="numeric" minute="numeric" />
<br />
{/* Weekday */}
<SlFormatDate weekday="long" />
<br />
{/* Month */}
<SlFormatDate month="long" />
<br />
{/* Year */}
<SlFormatDate year="numeric" />
<br />
{/* No formatting options */}
<SlFormatDate />
</>
);
Hour Formatting
기본적으로 브라우저가 12시간제 또는 24시간제를 결정해요. 하나로 강제하고 싶다면 hour-format 속성을 12 또는 24로 설정하면 돼요.
<sl-format-date hour="numeric" minute="numeric" hour-format="12"></sl-format-date><br />
<sl-format-date hour="numeric" minute="numeric" hour-format="24"></sl-format-date>
import SlFormatDate from '@shoelace-style/shoelace/dist/react/format-date';
const App = () => (
<>
<SlFormatDate hour="numeric" minute="numeric" hour-format="12" />
<br />
<SlFormatDate hour="numeric" minute="numeric" hour-format="24" />
</>
);
Localization
lang 속성으로 날짜/시간 포맷의 로케일을 지정할 수 있어요.
English: <sl-format-date lang="en"></sl-format-date><br />
French: <sl-format-date lang="fr"></sl-format-date><br />
Russian: <sl-format-date lang="ru"></sl-format-date>
import SlFormatDate from '@shoelace-style/shoelace/dist/react/format-date';
const App = () => (
<>
English: <SlFormatDate lang="en" />
<br />
French: <SlFormatDate lang="fr" />
<br />
Russian: <SlFormatDate lang="ru" />
</>
);
Importing
autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 건너뛰어도 돼요. 그 외에는 아래 스니펫 중 원하는 걸 골라 이 컴포넌트만 골라 가져올 수 있어요.
Script / Import / Bundler / React 방식이 준비되어 있어요.
CDN에서 <script> 태그로 이 컴포넌트를 가져오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/format-date/format-date.js"></script>
CDN에서 JavaScript import로 가져오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/format-date/format-date.js';
Bundler를 사용해 가져오려면:
import '@shoelace-style/shoelace/dist/components/format-date/format-date.js';
React 컴포넌트로 가져오려면:
import SlFormatDate from '@shoelace-style/shoelace/dist/react/format-date';
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
date |
The date/time to format. If not set, the current date and time will be used. When passing a string, |
it’s strongly recommended to use the ISO 8601 format to ensure timezones are handled correctly. To
convert a date to this format in JavaScript, use
`date.toISOString()`. | | `Date \| string` | `new Date()` |
| weekday | The format for displaying the weekday. | | 'narrow' \| 'short' \| 'long' | - |
| era | The format for displaying the era. | | 'narrow' \| 'short' \| 'long' | - |
| year | The format for displaying the year. | | 'numeric' \| '2-digit' | - |
| month | The format for displaying the month. | | 'numeric' \| '2-digit' \| 'narrow' \| 'short' \| 'long' | - |
| day | The format for displaying the day. | | 'numeric' \| '2-digit' | - |
| hour | The format for displaying the hour. | | 'numeric' \| '2-digit' | - |
| minute | The format for displaying the minute. | | 'numeric' \| '2-digit' | - |
| second | The format for displaying the second. | | 'numeric' \| '2-digit' | - |
| timeZoneName
time-zone-name | The format for displaying the time. | | 'short' \| 'long' | - |
| timeZone
time-zone | The time zone to express the time in. | | string | - |
| hourFormat
hour-format | The format for displaying the hour. | | 'auto' \| '12' \| '24' | 'auto' |
| updateComplete | A read-only promise that resolves when the component has
finished updating. | | | |
속성과 프로퍼티에 대해 더 알아보세요.
더 알아보기 (Learn more)
- Format Bytes 컴포넌트 — 바이트 포맷
- Format Number 컴포넌트 — 숫자 포맷
- Shoelace 컴포넌트 전체 문서 — 모든 컴포넌트와 가이드