Format dates in Svelte
How to format dates in Svelte with Intl.DateTimeFormat, locale and time zone options, and the time element, plus the readability, copy/paste and accessibility tradeoffs to check.
Svelte has no date formatter of its own. When people search for how to format a date in Svelte, the practical answer is to use JavaScript's built-in Intl.DateTimeFormat API, or a date library, inside a component. This review covers what formatting changes, when it works well, and where the formatted string can mislead readers or break after copying.
Is locale-aware date formatting the right choice?
For dates that people read on screen, locale-aware formatting is usually the right default. It turns a raw timestamp into something like "October 31, 2026" for US readers, and the same code adapts to other locales. The decisive tradeoff is that the output depends on locale, time zone and the JavaScript runtime. The same date can therefore render differently on the server, in different browsers, or for different users.
A fixed, unlocalized format such as ISO 8601 (2026-10-31) is the better choice in several cases: when the value will be parsed again, compared, sorted, logged or exported; when it goes into data files, URLs or API payloads; and when you need byte-for-byte stable output, as in tests and caches.
What formatting actually changes
Formatting does not change the date. A JavaScript Date represents a single instant. Formatting produces a new string that describes that instant for a particular locale and time zone. The stored value, whether a Date object, an ISO string or a timestamp in your data, stays as it was. The formatted text is presentation only, and it should not be treated as the source of truth.
"Format" can mean two different things here. One is choosing an order and style, such as month-day-year versus day-month-year, or numeric versus spelled-out months. That is a display decision. The other is converting between time zones. That changes which calendar day and clock time are shown, even though the instant is the same. Most surprising date bugs come from the second, not the first.
A minimal Svelte 5 component
<script>
let { date, locale = 'en-US' } = $props();
const formatter = $derived(
new Intl.DateTimeFormat(locale, { dateStyle: 'long' })
);
</script>
<time datetime={date.toISOString()}>{formatter.format(date)}</time>
Using $derived means the formatter is rebuilt only when the locale changes, not on every render. The time element keeps a machine-readable ISO value in its datetime attribute alongside the human-readable text.
The same idea in Svelte 4
<script>
export let date;
export let locale = 'en-US';
$: formatter = new Intl.DateTimeFormat(locale, { dateStyle: 'long' });
</script>
<time datetime={date.toISOString()}>{formatter.format(date)}</time>
Common options
- dateStyle and timeStyle: presets such as 'short', 'medium', 'long' and 'full'.
- Individual fields such as year, month, day, weekday, hour and minute, for finer control. These cannot be combined with dateStyle or timeStyle.
- timeZone: an IANA zone such as 'America/New_York' or 'UTC'. Without it, the runtime's local zone is used.
- hour12 or hourCycle: controls 12-hour versus 24-hour clocks.
- Intl.RelativeTimeFormat: a separate API for phrases like "3 days ago". You compute the difference yourself; the API only words it.
Library alternatives
Libraries such as date-fns and Day.js offer pattern-based formatting, for example a pattern like 'MMMM d, yyyy', plus helpers for date arithmetic. They help when you need an exact pattern or a lot of date math. The cost is an added dependency, and you still have to manage locale data and time zones. If you already use svelte-i18n, it provides date and time formatters tied to its current-locale store. That keeps date output consistent with the rest of your translated UI.
The time zone trap with date-only values
JavaScript parses a date-only ISO string such as "2026-03-05" as midnight UTC. A US user's browser then formats it in local time, which is several hours behind UTC, so the page can show March 4. If the value is a calendar date rather than an instant, such as a birthday, a due date or a publication day, either pass timeZone: 'UTC' when formatting or keep it as separate year, month and day values.
const d = new Date('2026-03-05');
// Formats in the runtime's local zone; in US zones this shows March 4
new Intl.DateTimeFormat('en-US', { dateStyle: 'long' }).format(d);
// Pins the calendar day
new Intl.DateTimeFormat('en-US', { dateStyle: 'long', timeZone: 'UTC' }).format(d);
Server rendering and hydration
In SvelteKit, a component can be rendered first on the server and then hydrated in the browser. If formatting relies on the default locale or time zone, the server uses its own settings and the browser uses the user's. The two outputs can differ, so the text may change after load, or the framework may report a mismatch.
There are three common ways to avoid this. You can pass an explicit locale and timeZone, derived from the request or a user setting. You can format only on the client and render a neutral value, such as the ISO date, on the server. Or you can accept server-side formatting and make sure the browser repeats it with the same explicit options.
Will the formatted date stay readable after copying or sharing?
Copying selects the visible text, not the datetime attribute. Whatever the user sees is what lands in an email, chat message or spreadsheet. Several properties of that text matter:
- Ambiguity: numeric dates such as 03/05/2026 read as March 5 in the US and as 3 May in many other regions. A spelled-out month removes the doubt once the text leaves your page.
- Hidden characters: recent versions of the ICU data used by browsers and Node.js insert a narrow no-break space (U+202F) before AM and PM in some English time formats. It looks like a normal space but is a different character. String comparisons, hardcoded test expectations and naive parsers can fail on it.
- Direction marks: formatted output for right-to-left locales can include invisible bidirectional control characters. They help the text display correctly, but they travel with copied text and can interfere with parsing or searching.
- Parsing back: formatted strings are not a reliable input format. Do not round-trip them through new Date(); keep the ISO value for anything a program needs to read.
Accessibility
Screen readers announce the visible text. A spelled-out format such as "October 31, 2026" is generally less ambiguous to hear than a string of slashes and digits. The datetime attribute on the time element is machine-readable metadata, and you should not rely on it to be announced. The exact way a particular screen reader voices a given date string was not tested for this review. If that matters for your audience, check with the specific assistive technology and browser combinations your users rely on.
Search and editing
Text search within a page or app matches the formatted string. A user looking for "2026-10-31" will not find "October 31, 2026". If dates need to be searchable or filterable, index the stored ISO value rather than the display text. For editable fields, the native date input works with ISO values internally and shows a browser-localized control. That avoids parsing user-typed formatted dates altogether.
Choosing a format
| Use case | Recommended output |
| Dates shown to US readers | Intl.DateTimeFormat with dateStyle 'long' or 'medium' |
| Calendar dates without a time | Intl.DateTimeFormat with timeZone 'UTC', or separate date parts |
| Multi-language UI | svelte-i18n or Intl with the active locale passed explicitly |
| Exports, APIs, logs, tests | ISO 8601 string, not localized |
| Machine-readable markup | time element with an ISO datetime attribute |
Gaps in this review
No performance measurements, bundle-size figures or cross-browser rendering tests are reported here. Where behavior depends on the runtime, such as ICU spacing characters, bidi marks and hydration warnings, confirm it in the environments you ship to rather than assuming one successful render applies everywhere.