Sub topic 8
time and data for machine-readable values
Some values in a page mean more to a program than they do in the way people write them. "30 November" has no year. "Next Tuesday" depends on when you read it. "£4.50" is clear to a shopper but tells a script nothing about the currency. The time and data elements let you keep the human-readable text and attach a precise value that software can read.
This chapter assumes you know basic HTML syntax, how attributes work, and how to inspect an element in your browser's developer tools. It builds on the text-level elements from the previous chapter, such as strong, em and abbr.
The problem: text written for people
People write dates and quantities in many forms. A script that reads page text has to guess which form it is looking at.
3/4/2026is 3 April in most of the world and 4 March in the United States.Tue 8pmhas no date or time zone.twelve hundredand1,200and1.200can all mean the same number, or not, depending on locale.
You could change the visible text to a strict format, but that makes the page harder for readers. The better approach is to leave the visible text alone and add a second, strict representation in an attribute. The element carries both: text for people, a value for machines.
The time element
time represents a date, a time, or a duration. The visible content is what readers see. The datetime attribute holds the machine-readable form.
<p>
Published <time datetime="2026-11-30">30 November 2026</time>
by Sam Lee.
</p>The reader sees "30 November 2026". A browser extension, a calendar tool, a search engine or your own script can read 2026-11-30 from datetime without parsing prose.
Formats that datetime accepts
The datetime value must follow one of the formats defined by the HTML standard. They are based on ISO 8601 but are a stricter subset, so not every ISO 8601 string is valid.
| What it represents | Example value | Notes |
|---|---|---|
| Date | `2026-11-30` | Year, month, day |
| Month | `2026-11` | No day |
| Year | `2026` | Four or more digits |
| Yearless date | `11-30` | Month and day, for recurring dates like birthdays |
| Week | `2026-W49` | ISO week number |
| Time | `19:30` or `19:30:15.250` | 24-hour clock, seconds optional |
| Local date and time | `2026-11-30T19:30` | `T` or a space between date and time |
| Date and time with offset | `2026-11-30T19:30+01:00` | Offset from UTC, or `Z` for UTC |
| Duration | `PT2H30M` | `P` starts a duration, `T` starts the time part |
Durations have a second, longer form that uses spaces, such as 2h 30m. The ISO form is the safer choice because more tools understand it.
<p>
The workshop starts at
<time datetime="2026-12-05T09:30+00:00">9:30 am UTC on 5 December</time>
and runs for
<time datetime="PT2H30M">two and a half hours</time>.
</p>When you can skip datetime
If the text inside time is already in a valid format, datetime is optional. The element's content is then used as the value.
<p>Doors open at <time>19:30</time>.</p>If the text is anything else, such as "yesterday" or "30 November", you must provide datetime. Without it, the element has no machine-readable value, and the markup has gained nothing over a plain span.
Is it a good fit?
time is for a specific, valid point or span of time. It is not for every mention of a date.
- Use it for publication dates, event start times, deadlines, and durations in a recipe.
- A date before year 1 in the Gregorian calendar cannot be expressed. Historical dates such as "around 300 BC" do not fit.
- Fuzzy values like "late spring" or "a few weeks ago" have no valid
datetime. Leave them as plain text.
Using time in scripts
Because the value sits in an attribute, a script can read it reliably. This example rewrites each date into the reader's locale and leaves the original text if the value is invalid.
<p>Published <time datetime="2026-11-30">30 November 2026</time></p>
<script>
for (const el of document.querySelectorAll("time[datetime]")) {
const date = new Date(el.dateTime);
if (Number.isNaN(date.getTime())) continue;
el.textContent = new Intl.DateTimeFormat(undefined, {
dateStyle: "long",
timeZone: "UTC"
}).format(date);
}
</script>el.dateTime is the DOM property that reflects the datetime attribute. The timeZone: "UTC" option stops a date-only value from shifting to the previous day for readers west of Greenwich, because new Date("2026-11-30") is parsed as midnight UTC.
The pubdate attribute is gone
Older drafts of HTML5 had a pubdate attribute and a different time definition. It was removed from the standard. Do not use it. To mark the publication date of an article, use time inside the article's header or footer, and consider structured data if you need search engines to understand which date is which.
The data element
data links content to a machine-readable value that is not a date or time. The value attribute is required, and the content is what readers see.
<ul>
<li><data value="9780141036144">Nineteen Eighty-Four</data></li>
<li><data value="9780141439518">Pride and Prejudice</data></li>
</ul>Here, the visible titles map to ISBN values. A script can find an element, read el.value, and look up the book without matching on title strings.
Other uses include product identifiers, numeric amounts that appear in words, and database keys.
<p>
Price:
<data value="4.50">four pounds fifty</data>
</p>What data does not do
Browsers do not display the value, do not use it for any built-in feature, and assistive technology generally ignores it. The data element changes nothing for a reader on its own. Its purpose is to carry values for your own scripts, tests and tools. Before reaching for it, ask whether a more specific mechanism fits better:
- A date or time: use
time. - A number shown as a gauge: use
meterorprogress. - A value in a form: use an
inputwith avalue. - Data for a script that does not belong to one visible piece of text: use a
data-*attribute. - Data you want search engines to understand as a product, event or recipe: use structured data such as JSON-LD, which has agreed vocabularies.
The data element is a reasonable choice when the value belongs to a specific piece of visible text and you want the element name itself to say so. A data-* attribute on a span does the same job and is more common in practice. Neither is wrong. What matters is that the value is attached to the text it describes.
data versus data-* attributes
| Feature | `<data value="...">` | `data-*` attribute |
|---|---|---|
| Element has meaning | Yes: "this content has a machine value" | No: works on any element |
| Number of values | One | Any number, with separate names |
| Script access | `el.value` | `el.dataset.name` |
| Valid on any element | No, it is its own element | Yes |
Use data when you have exactly one value for a piece of inline text. Use data-* attributes when you need several values or the value belongs to a larger element such as a li or article.
What each element exposes
Both elements are inline, so they sit inside paragraphs and list items without breaking the flow. Neither has a visual style by default. In the accessibility tree, time and data are mapped to generic text, so a screen reader reads the visible content, not the datetime or value. This is a feature, not a flaw: the spoken text is the text you wrote for people.
Do not put a different meaning in the attribute than the visible text claims. If the text says "30 November" and datetime says 2026-12-30, you have created a mismatch that readers cannot detect and tools will believe.
Common mistakes
- Wrapping vague text such as "recently" in
timewith an invented date. - Writing a
datetimethat is not valid, such as30/11/2026or2026-11-30 7pm. Parsers will ignore it. - Leaving off the time zone offset on an event that happens at a specific moment worldwide.
- Using
datawheretimefits. - Expecting
dataortimeto change how text is displayed or read aloud. - Repeating the whole date in
datetimewhen only the year is shown to readers. The value should match what the text states, not more.
Checking your work
In the Elements panel of your browser's developer tools, select a time element and confirm that datetime matches the visible text. In the console, document.querySelector("time").dateTime returns the attribute value, and new Date(value) shows how a script will interpret it. The W3C Nu HTML Checker reports an invalid datetime value as an error, which makes it the quickest way to find a malformed date.
Summary
Use time for valid dates, times and durations, and give it a datetime unless the content is already in a valid format. Use data with value to attach a single machine-readable value to inline text. Keep the visible text written for people, keep the attribute strict, and make sure the two agree.
Exercise
A blog post says "Posted last Friday" and the post was published on 27 November 2026 at 14:00 in UTC+1. Write the markup, then explain why the visible text works with time only if you add an attribute.
Show answer
<p>Posted <time datetime="2026-11-27T14:00+01:00">last Friday</time></p>. The text "last Friday" is not a valid date or time format, so the browser and any tool cannot get a value from it. The datetime attribute supplies the value in a valid format, including the date, the time and the UTC offset. Without it, the element has no machine-readable value.
Exercise
A shop lists a product as "Blue mug" with the internal stock code MUG-0042. Which is more suitable for a script that needs the code: <data value="MUG-0042">Blue mug</data> or <time datetime="MUG-0042">Blue mug</time>? Explain why, and name one alternative if the product row also needs a price and a weight.
Show answer
The data element is correct. A stock code is not a date, time or duration, so time would be invalid and parsers would ignore the value. If the row needs several values, put data-code, data-price and data-weight attributes on the containing element, because the data element holds only one value.