Format date

A utility component for formatting dates and times using the browser's Intl.DateTimeFormat API, with automatic locale support.

Overview

sl-format-date renders a formatted date or time string based on a date property and a set of formatting options. It wraps the native Intl.DateTimeFormat API, so all standard formatting options are available as element properties.

When no valid date is provided, the element falls back to rendering its slotted content — useful for showing a placeholder or an error message.

<sl-format-date date="2026-04-01" time-style></sl-format-date>

Usage

Date and time styles

The quickest way to format a date is to use date-style and/or time-style. These map directly to Intl.DateTimeFormatOptions.

StyleExample output
fullWednesday, April 9, 2025
longApril 9, 2025
mediumApr 9, 2025
short4/9/25
<sl-format-date date-style="long" time-style="short"></sl-format-date>

Individual date/time parts

For precise control you can set individual part properties. When any of these are set, date-style and time-style are ignored. See the Properties table below for all available options.

Locale

The component uses LocaleMixin, which automatically picks up the locale from the nearest lang attribute in the DOM. This means the formatted output adapts to the language and regional conventions of the surrounding page without any extra configuration. You can also override it explicitly with the locale property:

<sl-format-date locale="nl" date-style="long"></sl-format-date>

Advanced formatting options

For formatting options not exposed as individual properties, use date-time-options to pass an Intl.DateTimeFormatOptions object directly:

const el = document.querySelector('sl-format-date');
el.date = new Date();
el.dateTimeOptions = { calendar: 'hebrew' };

Fallback content

When date is not set or is invalid, the default slot is rendered. Use this to show a placeholder or error message:

<sl-format-date>No date available</sl-format-date>

API

Properties

PropertyAttributeTypeDescription
datedateDate | string | number | null | undefinedThe date to format. Accepts a Date object, a date string, or a timestamp. Falls back to slot content when invalid or unset.
dateStyledate-style'full' | 'long' | 'medium' | 'short'Shorthand date format. Used together with timeStyle. Ignored when individual date/time part properties are set.
timeStyletime-style'full' | 'long' | 'medium' | 'short'Shorthand time format. Used together with dateStyle. Ignored when individual date/time part properties are set.
weekdayweekday'narrow' | 'short' | 'long'How to display the weekday.
eraera'narrow' | 'short' | 'long'How to display the era.
yearyear'numeric' | '2-digit'How to display the year.
monthmonth'numeric' | '2-digit' | 'narrow' | 'short' | 'long'How to display the month.
dayday'numeric' | '2-digit'How to display the day.
dayPeriodday-period'narrow' | 'short' | 'long'How to display the day period (AM/PM). Only effective when hour12 is true.
hourhour'numeric' | '2-digit'How to display the hour.
minuteminute'numeric' | '2-digit'How to display the minute.
secondsecond'numeric' | '2-digit'How to display the second.
timeZoneNametime-zone-name'short' | 'long'How to display the time zone name.
timeZonetime-zonestringThe IANA time zone to use (e.g. Europe/Amsterdam). Defaults to the runtime's local time zone.
hour12hour12booleanWhether to use 12-hour time. Defaults to locale-dependent behaviour.
localelocalestringThe BCP 47 locale tag to use. Inherits from the nearest lang attribute by default.
dateTimeOptionsdate-time-optionsIntl.DateTimeFormatOptionsAdvanced formatting options passed directly to Intl.DateTimeFormat. These are merged with and can override the individual property values.

Slots

NameDescription
(default)Fallback content shown when date is not set or is invalid.

Static properties

The default values of dateStyle and timeStyle can be changed globally for all future instances:

import { FormatDate } from '@sl-design-system/format-date';

FormatDate.dateStyle = 'short';
FormatDate.timeStyle = 'short';