Sub topic 8
time and data for machine-readable values
This chapter assumes you know basic HTML syntax and the text-level elements such as strong and em. Both elements here do the same job. They keep the text people read and add a value that software can parse reliably.
Why a machine-readable value helps
People write dates and quantities in many forms: "30 Nov", "yesterday", "11/12/2026" (which is November or December, depending on locale). A browser, a search engine, or a script cannot always tell which one is meant. The two elements let you show the human text and attach a precise value next to it.
The time element
time marks a date, a time, or a duration. The visible text can be anything. The datetime attribute holds the value in a fixed format.
<p>
Posted on
<time datetime="2026-11-30">30 November 2026</time>.
</p>
<p>
Doors open at <time datetime="07:30">7:30 am</time>.
</p>
<p>
Reading time: <time datetime="PT12M">12 minutes</time>.
</p>If the element's text content is already in a valid format, you can leave out datetime: <time>2026-11-30</time>.
Common datetime formats
| Meaning | Example value |
|---|---|
| Date | `2026-11-30` |
| Month | `2026-11` |
| Week | `2026-W49` |
| Time | `07:52` |
| Date and time with UTC | `2026-11-30T07:52Z` |
| Date and time with offset | `2026-11-30T09:52+02:00` |
| Duration | `PT2H30M` |
A duration starts with P. Time parts come after a T, so PT2H30M means two hours and thirty minutes.
<p>
The webinar starts
<time datetime="2026-12-03T15:00Z">3 December, 15:00 UTC</time>
and runs for <time datetime="PT1H30M">90 minutes</time>.
</p>The data element
data is the general version. Its value attribute holds a machine-readable form of the content, for any kind of value that is not a date or time.
<ul>
<li><data value="9780141036144">Nineteen Eighty-Four</data></li>
<li><data value="9780060850524">Brave New World</data></li>
</ul>
<p>
Price: <data value="19.99">$19.99</data>
</p>Here the value holds an ISBN or a plain number without the currency symbol. A script can read it with element.value or element.getAttribute("value"), so it does not need to parse the display text.
<script>
const price = document.querySelector("data");
console.log(Number(price.value) * 2); // 39.98
</script>If the content is a date or time, use time instead of data.
What the elements do and do not do
- Browsers show the text content as normal text. Neither element adds default styling.
- Neither element is a landmark or a heading. Assistive technology generally reads the visible text, not the attribute value.
- Search engines and other parsers can use the values. Calendar tools and scrapers can read them too.
- You can style them with CSS or find them with
querySelectorAll("time")like any other element.
Common errors
- Using
timefor something that is not a date, time, or duration, such as a version number. Usedatafor that. - Writing a local time without an offset when the audience is global. Add
Zor an offset. - Wrapping a whole sentence in
time. Wrap only the date or time itself.
Exercise
A product page shows "Free trial: 14 days" and "Release date: 5 March 2026". Which element and value would you use for each?
Show answer
Use <time datetime="P14D">14 days</time> for the trial, because it is a duration. Use <time datetime="2026-03-05">5 March 2026</time> for the release date. If the trial length were a plain count, such as a SKU or a rating, data with a value would fit instead.