The Placer Toolkit alpha has
arrived! 🎉
Plenty of changes are coming your
way—some big, some breaking, some
even nuclear! Beware the changelog…
Format Date
<pc-format-date> Formats a date/time using the specified locale and options.
Localisation is handled by the browser’s Intl.DateTimeFormat API. No language packs are required.
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/2024, which can be interpreted as 4 March 2024 or 3 April 2024 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.
Demos#
Date and 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 localised, numeric date will be displayed instead.
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.
Localisation#
Use the lang attribute to set the date/time formatting locale.
Properties#
| Name | Description | Reflects | 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().Type: Date | string |
| new Date() |
weekday | The format for displaying the weekday. Type: "narrow" | "short" | "long" | undefined |
| ‐ |
era | The format for displaying the era. Type: "narrow" | "short" | "long" | undefined |
| ‐ |
year | The format for displaying the year. Type: "numeric" | "2-digit" | undefined |
| ‐ |
month | The format for displaying the month. Type: "numeric" | "2-digit" | "narrow" | "short" | "long" | undefined |
| ‐ |
day | The format for displaying the day. Type: "numeric" | "2-digit" | undefined |
| ‐ |
hour | The format for displaying the hour. Type: "numeric" | "2-digit" | undefined |
| ‐ |
minute | The format for displaying the minute. Type: "numeric" | "2-digit" | undefined |
| ‐ |
second | The format for displaying the second. Type: "numeric" | "2-digit" | undefined |
| ‐ |
timeZoneNametime-zone-name | The format for displaying the time. Type: "short" | "long" | undefined |
| ‐ |
timeZonetime-zone | The time zone to express the time in. Type: string | undefined |
| ‐ |
hourFormathour-format | The format for displaying the hour. Type: "auto" | "12" | "24" |
| "auto" |
updateComplete | A read‐only promise that resolves when the component has finished updating. | ‐ |
Learn more about attributes and properties.
Importing#
If you’re using the autoloader or the standard loader, you can skip this section. But if you’re cherry picking, you can use any of the following snippets to import this component.
To manually import this component from the CDN, copy this code snippet and paste it in your HTML.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.1/cdn/components/format-date/format-date.js"></script> To manually import this component from the CDN, copy this code snippet and paste it in your JavaScript file.
import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.1/cdn/components/format-date/format-date.js"; To manually import this component from npm, copy this code snippet and paste it in your JavaScript file.
import "placer-toolkit/dist/components/format-date/format-date.js";