Locale-aware date and relative time formatting via data attributes.
Overview
The data-format-date attribute enhances native <time> elements by replacing their text content with a locale-aware formatted date. Supports both absolute date formats and auto-refreshing relative time strings.
1
<time datetime="2026-02-09" data-format-date>Feb 9, 2026</time>
How It Works
- Reads the
datetime attribute from the <time> element
- For absolute styles: formats using
Intl.DateTimeFormat with the specified dateStyle, and keeps the authored text as the title tooltip
- For
"relative": formats using Intl.RelativeTimeFormat, puts the exact date and time in title, and starts an adaptive auto-refresh
- Formats again whenever
datetime, data-format-date, data-format-time, data-time-zone, data-locale or a lang attribute changes
The locale is resolved in this order: data-locale on the element, the nearest ancestor with a lang attribute (so a <section lang="de"> formats its dates in German), <html lang>, then the browser language.
Attributes
| Attribute |
Type |
Description |
data-format-date |
string |
Date style: "" / "medium" (default), "short", "long", "full", "relative". |
datetime |
string |
Standard HTML datetime attribute. Must be a valid date string (ISO 8601 recommended). An invalid or missing value leaves the text as written. |
data-format-time |
string |
Optional time style: "short", "medium", "long", "full". Ignored for "relative" and for date-only values. |
data-time-zone |
string |
Optional IANA time zone (e.g. "Europe/Paris") for date-time values. Default: the viewer's zone. Unknown zones are ignored. Date-only values always format as calendar dates. |
data-locale |
string |
Optional locale override (e.g., "de-DE"). Defaults to the nearest [lang], then <html lang>, then the browser locale. |
title |
string |
Set automatically unless you author one: the original text for absolute styles, the exact date and time for "relative". |
data-format-date-init |
boolean |
State hook, set once the element is enhanced. Read it for styling or tests; do not set it. Enhancement is tracked internally, so a cloned element is still formatted when it is inserted. |
Absolute Date Styles
Date styles map directly to Intl.DateTimeFormat's dateStyle option:
1234567891011
<time datetime="2026-02-09" data-format-date>Feb 9, 2026</time> <time datetime="2026-02-09" data-format-date="short">2/9/26</time> <time datetime="2026-02-09" data-format-date="long">February 9, 2026</time> <time datetime="2026-02-09" data-format-date="full">Monday, February 9, 2026</time>
Date-Only Values
A datetime with no time part names a calendar date, not an instant. VB formats YYYY, YYYY-MM and YYYY-MM-DD values in UTC, so 2026-02-09 shows 9 February in every time zone (a plain new Date('2026-02-09') would show 8 February west of UTC). The precision is kept: a year shows only the year, a month shows month and year, and data-format-time is ignored.
123
<time datetime="2026" data-format-date>2026</time> <time datetime="2026-02" data-format-date="long">Feb 2026</time> <time datetime="2026-02-09" data-format-date>Feb 9, 2026</time>
With Time
Add data-format-time to include the time component:
123
<time datetime="2026-02-09T14:30:00Z" data-format-date="long" data-format-time="short" data-time-zone="UTC"> February 9, 2026 at 2:30 PM</time>
Time Zones
Values with a time (2026-02-09T14:30Z) are instants. By default they format in the viewer's time zone, so a reader in Tokyo and one in New York see different clock times for the same moment. When the time belongs to a place — a meetup in Paris, a store's opening hours — set data-time-zone, and add data-format-time="full" if readers need to see the zone name.
12
<time datetime="2026-03-12T18:30:00Z" data-format-date="long" data-format-time="full" data-time-zone="Europe/Paris">12 March 2026, 19:30 Paris time</time>
Relative Time
Use data-format-date="relative" for auto-refreshing relative time strings like "3 hours ago" or "in 2 days". The exact date and time go into the title tooltip. Write the real date as the fallback text, so readers without JavaScript still see when it happened.
1
<time datetime="2026-02-09T10:00:00Z" data-format-date="relative">February 9, 2026</time>
The demo stamps its datetime values relative to the moment you open it, so the phrases stay current. A real page renders datetime on the server.
Auto-Refresh Intervals
Relative times update automatically on an adaptive schedule:
| Age of timestamp |
Refresh interval |
| < 1 hour |
Every 60 seconds |
| < 24 hours |
Every 5 minutes |
| > 24 hours |
Every hour |
Timers stop when an element is removed from the DOM. Units round up to the next larger one when they reach it, so 350 days reads "last year", not "12 months ago".
Locale Override
Use data-locale to force a specific locale for formatting:
123
<time datetime="2026-02-09" data-format-date="long" data-locale="de-DE">9. Februar 2026</time><time datetime="2026-02-09" data-format-date="long" data-locale="ja-JP">2026年2月9日</time><time datetime="2026-02-09" data-format-date="long" data-locale="ja-JP-u-ca-japanese">令和8年2月9日</time>
Plain ja-JP formats with the Gregorian calendar. Add the BCP 47 calendar extension -u-ca-japanese for era dates; data-format-date="short" then gives the form-field abbreviation R8/2/9. See Internationalization for setting it page-wide.
Common Use Cases
Activity Feeds
Show when events happened in a list (live in the relative-time demo above):
12345678910
<ul> <li> Alice commented <time datetime="2026-02-09T09:55:00Z" data-format-date="relative">9 Feb 2026, 09:55</time> </li> <li> Bob pushed a commit <time datetime="2026-02-09T08:00:00Z" data-format-date="relative">9 Feb 2026, 08:00</time> </li></ul>
Content Metadata
Display article or post timestamps:
123456
<article> <h2>Getting Started with Vanilla Breeze</h2> <p><small> Published <time datetime="2026-02-08T10:00:00Z" data-format-date="relative">8 February 2026</time> </small></p></article>
Dynamic Elements
Elements added to the DOM after page load are automatically enhanced via a MutationObserver. No manual initialization is needed. Changing datetime later re-formats the element (and restarts a relative refresh). A relative element that is removed and reinserted (for example when a list is re-sorted) picks up a fresh refresh timer, and a cloned element is formatted like a new one.
123456789
const time = document.createElement('time');time.setAttribute('datetime', new Date().toISOString());time.dataset.formatDate = 'relative';time.textContent = 'just now';document.body.appendChild(time); time.setAttribute('datetime', '2026-03-01T09:00:00Z');
Progressive Enhancement
The <time> element's original text serves as a fallback when JavaScript is unavailable. Users always see a readable date, with formatting layered on top.
1
<time datetime="2026-02-09" data-format-date>Feb 9, 2026</time>
Accessibility
- The visible text is what screen readers announce;
title is not reliably exposed. Keep the visible text unambiguous, and use an absolute style where the exact date matters.
- For relative times the exact date and time are in the
title tooltip, and the cursor: help style hints that it is there.
- Relative refreshes rewrite the text silently: there is no live region, so a page full of timestamps does not interrupt a screen-reader user every minute.
- Mainstream screen readers announce the visible text, not the
datetime value. datetime serves search engines, browser features and scripts.
- Falls back to the original text content when JavaScript is unavailable
- No wrapper element — the
<time> is the enhanced element, keeping the accessibility tree clean
Related
<time> — the element this attribute enhances.
- Choosing a relative-time mechanism —
data-format-date="relative" versus time[data-relative]. Use data-format-date="relative" for new content: it handles future dates and every locale and puts the exact date in the tooltip. data-relative is the lightweight past-only formatter that <activity-feed>, <comment-thread> and <time-index> render.
<time-index> — a dated index of entries.
data-format-number and data-format-bytes — the same locale-aware formatting for numbers and file sizes.
datetime — the machine-readable value this attribute formats, and the date and time strings it accepts.
lang — the attribute the formatter reads for its locale when data-locale is not set.