skip to content

In HTML, what does the datetime attribute on the <time> element do, and when may you omit it?

level: middleimportance: should knowfreq 42%

answer

  1. human text stays, machine value beside it
  2. the attribute is the parseable half
  3. durations start with P
  4. omit it only when the text parses
  5. no formatting, no localisation

basics

~20 s

The datetime attribute holds a machine-readable version of the date, time or duration the element's visible text describes, using the formats HTML defines. You may omit it only when the element's own text is already a valid one of those strings.

solid answer

~40 s

`<time>` marks a date, a time, or a duration, and `datetime` carries the machine-readable form of whatever the human-readable text says — `<time datetime="2026-03-05">March 5</time>`. The value must be a valid HTML date/time string: a date, a time, a local date-time, a year or month, a week such as `2026-W10`, a global date-time with a `Z` or an offset, or a duration such as `PT2H30M`. If the element's own text content is already in one of those formats, `datetime` is optional. What `<time>` does not do is any formatting: browsers do not localise it, it renders as ordinary text, and assistive technology reads the visible text rather than the attribute. The attribute exists for machines — crawlers, feed generators, and your own scripts.

code

html · 4 lines
html
<p>Published <time datetime="2026-03-05">March 5, 2026</time>.</p>
<p>Doors open <time datetime="2026-03-05T19:00-05:00">7pm Eastern</time>.</p>
<p>Prep time: <time datetime="PT2H30M">two and a half hours</time>.</p>
<p><time>2026-03-05</time> needs no datetime attribute.</p>

go deeper

for a junior

Know that <time> wraps a date or time and that datetime holds the machine-readable version, and be able to write one correct example from memory.

for a middle

Explain the value grammar — date, time, local versus global date-time, week, duration — and state the one case where the attribute may be omitted because the text itself parses.

for a senior

Show judgment about consumers: pick global values when time zones matter, keep the visible text unambiguous for screen-reader users, and validate values rather than trusting that a bad one will fail loudly.

for a principal

Own how dates travel through the system: one canonical instant rendered into markup, a rule for which values are localised in text, and a check that keeps machine-readable values from drifting out of sync with the prose.

## The problem `<time>` solves Human-readable dates are ambiguous and endlessly varied: "March 5", "5/3", "last Tuesday", "in two hours". Machines that want to read a page — search crawlers, feed generators, calendar importers, your own scripts — cannot parse all of that reliably. `<time>` lets you keep the prose you actually want to show and attach the unambiguous value beside it: ```html <p>The workshop starts <time datetime="2026-03-05T14:30-05:00">Thursday at 2:30pm</time>.</p> ``` The visible text is for people; `datetime` is for machines. Neither constrains the other, which is the whole point. ## The permitted values `datetime` must be a **valid date/time string** as HTML defines it, and the vocabulary is wider than most people expect: - a date — `2026-03-05` - a month — `2026-03` - a year — `2026` - a week — `2026-W10` - a yearless date — `03-05` - a time — `14:30`, `14:30:15`, `14:30:15.500` - a local date and time — `2026-03-05T14:30` - a global date and time, with `Z` or a numeric offset — `2026-03-05T14:30Z`, `2026-03-05T14:30-05:00` - a time-zone offset on its own — `-05:00` - a duration — `PT2H30M`, `PT30M`, `P3D` Durations use the `P…T…` form: `P` opens the period, `T` separates the date part from the time part, so two and a half hours is `PT2H30M`, not `2:30` and not `T2H30M`. A space-separated duration form is also permitted (`2h 30m`), but the `PT` form is the one to say in an interview because it is unambiguous and universally accepted. A value the parser cannot read is simply not a valid `<time>` value — you do not get a browser error, you get silence, which is why validators matter here. ## When you may omit the attribute If the element's own text content is already a valid date/time string, `datetime` is redundant: ```html <time>2026-03-05</time> <!-- fine, text is machine-readable --> <time>March 5, 2026</time> <!-- no machine-readable value at all --> ``` The second example is the trap. Nothing errors, nothing renders differently, and a naive reviewer sees `<time>` and assumes the page is machine-readable. It is not: browsers do not parse English dates out of text content. ## What `<time>` does not do Three over-claims come up constantly: - **It does not format or localise.** There is no built-in "render this in the user's locale" behaviour. If you want a localised string you produce it yourself and put it in the text content. - **It does not produce a relative time.** "3 hours ago" is text you compute; the element only records what the absolute value is. - **It has no default assistive-technology behaviour.** A screen reader reads the visible text, not the attribute. So if the visible text is `5/3` and you meant March 5, the attribute does not rescue the reader — fix the text. It also has no default styling of its own, and it is phrasing content, so it sits inside a sentence like any other inline element. ## Where it pays off The common wins are content pipelines: an article's publication date that a feed builder or crawler can read, an event listing whose start and end are unambiguous, a recipe's prep duration, or a `<time>` inside a comment that your client-side code reads to render "2 hours ago" without re-parsing prose. It is also the honest markup even when nothing consumes it yet, because it costs one attribute and makes the intent explicit. ## Pitfalls to name Putting a display string in `datetime` (`datetime="March 5"`), which is invalid. Using `<time>` for things that are not dates or times — a duration is fine, but a version number or a page count is not. Mixing a local date-time with a text that implies a time zone, leaving readers in another region to guess: if the zone matters, use the global form with `Z` or an offset. And assuming `<time>` gives you structured data on its own — it is a machine-readable value in the document, not a schema.

  • How would you mark up a relative timestamp like "3 hours ago" in a comment list?
    Put the relative phrase in the visible text and the absolute instant in `datetime`: `<time datetime="2026-03-05T14:30Z">3 hours ago</time>`. The element records the fact that does not drift; the text is what the reader wants. Client code can re-render the text later from the attribute without parsing prose, and a crawler still gets an unambiguous value.
  • Does <time> improve SEO on its own?
    Not by itself. It records an unambiguous value in the markup, which is strictly better than prose for anything that parses the page, but it is not a schema and it makes no promise about how any particular consumer treats it. If a search feature requires structured data, that is a separate representation; `<time>` is the honest in-document value underneath it.
  • What is the difference between a local and a global date-time value in the datetime attribute?
    A local value like `2026-03-05T14:30` names a wall-clock time with no zone, so it means different instants in different places. A global value adds `Z` or a numeric offset — `2026-03-05T14:30-05:00` — and pins an exact instant. Use the global form whenever a reader in another region could get it wrong.

saying these in an interview costs you the question

  • Thinks <time> localises or formats the date automatically
  • Puts display text such as "March 5" in datetime
  • Believes screen readers read the attribute instead of the text
  • Writes durations as 2:30 rather than PT2H30M
  • Assumes <time> alone counts as structured data

context