Format Date
<sc-format-date> | ScFormatDate
Formats a date/time using the specified locale and options.
Localization is handled by the browser’s
Intl.DateTimeFormat
API. No language packs are required.
<!-- Shoelace 2 release date 🎉 --> <sc-format-date date="2020-07-15T09:17:00-04:00"></sc-format-date>
import ScFormatDate from '@styledc-dev/styledc/dist/react/format-date'; const App = () => <ScFormatDate date="2020-07-15T09:17:00-04:00" />;
The date
attribute determines the date/time to use when formatting. It must be a string that
Date.parse()
can interpret or a
Date
object set via JavaScript. If omitted, the current date/time will be assumed.
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
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. 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.
<!-- Human-readable date --> <sc-format-date month="long" day="numeric" year="numeric"></sc-format-date><br /> <!-- Time --> <sc-format-date hour="numeric" minute="numeric"></sc-format-date><br /> <!-- Weekday --> <sc-format-date weekday="long"></sc-format-date><br /> <!-- Month --> <sc-format-date month="long"></sc-format-date><br /> <!-- Year --> <sc-format-date year="numeric"></sc-format-date><br /> <!-- No formatting options --> <sc-format-date></sc-format-date>
import ScFormatDate from '@styledc-dev/styledc/dist/react/format-date'; const App = () => ( <> {/* Human-readable date */} <ScFormatDate month="long" day="numeric" year="numeric" /> <br /> {/* Time */} <ScFormatDate hour="numeric" minute="numeric" /> <br /> {/* Weekday */} <ScFormatDate weekday="long" /> <br /> {/* Month */} <ScFormatDate month="long" /> <br /> {/* Year */} <ScFormatDate year="numeric" /> <br /> {/* No formatting options */} <ScFormatDate /> </> );
Hour Formatting
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
.
<sc-format-date hour="numeric" minute="numeric" hour-format="12"></sc-format-date><br /> <sc-format-date hour="numeric" minute="numeric" hour-format="24"></sc-format-date>
import ScFormatDate from '@styledc-dev/styledc/dist/react/format-date'; const App = () => ( <> <ScFormatDate hour="numeric" minute="numeric" hour-format="12" /> <br /> <ScFormatDate hour="numeric" minute="numeric" hour-format="24" /> </> );
Localization
Use the lang
attribute to set the date/time formatting locale.
French:
Russian:
English: <sc-format-date lang="en"></sc-format-date><br /> French: <sc-format-date lang="fr"></sc-format-date><br /> Russian: <sc-format-date lang="ru"></sc-format-date>
import ScFormatDate from '@styledc-dev/styledc/dist/react/format-date'; const App = () => ( <> English: <ScFormatDate lang="en" /> <br /> French: <ScFormatDate lang="fr" /> <br /> Russian: <ScFormatDate lang="ru" /> </> );
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/format-date/format-date.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/format-date/format-date.js';
To import this component using a bundler:
import '@styledc-dev/styledc/dist/components/format-date/format-date.js';
To import this component as a React component:
import ScFormatDate from '@styledc-dev/styledc/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 about attributes and properties.