HTML datetime Attribute

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The datetime attribute gives a machine-readable date, time, or duration while the element’s text stays easy for people to read. It is used on <time>, <ins>, and <del>.

On <time> it marks when something happens. On <ins> / <del> it records when content was added or removed (often with cite for a change URL).

Baseline · Widely available.

Not deprecated and not experimental. The <time> element and datetime are stable across modern browsers.

Deprecated: pubdate.

The old pubdate boolean on <time> is obsolete. Use datetime for the precise value instead.

HTML Syntax

syntax.html
<time datetime=" value">human text</time>

Same attribute name on <ins> and <del>. The value must match a valid date, time, datetime, week, year, or duration string.

Attribute Values

Common valid forms (HTML living standard / ISO 8601 style):

ValueDescriptionTry it
YYYY-MM-DDFull date, e.g. 2018-07-07Try it
HH:MM / HH:MM:SSTime of day, e.g. 20:00Try it
YYYY-MM-DDTHH:MMLocal date and time; optional Z or ±HH:MM offsetTry it
PT2H30MDuration (ISO 8601), e.g. 2 hours 30 minutesTry it
YYYY-MM / YYYY-WwwMonth or week string, e.g. 2018-05, 2013-W46Try it

Live Preview

Pick a datetime value. The visible text stays readable; the attribute holds the precise value.

Updates a real <time> element and shows dateTime from the DOM.

time element

Event on .

datetime="2018-07-07" · dateTime = "2018-07-07"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Friendly Text, Precise Date

Show “July 7” to readers; keep 2018-07-07 for machines.

HTML
<p>
  The concert is on
  <time datetime="2018-07-07">July 7</time>
  in London.
</p>
Try It Yourself

How It Works

  1. Wrap the date in <time>.
  2. Put ISO in datetime. YYYY-MM-DD is unambiguous.
  3. Keep readable text inside the element for humans.
  4. Machines read the attribute — useful for search and calendars.

Example 2 — Clock Time and Duration

Mark a start time and how long something lasts.

HTML
<p>
  Starts at <time datetime="20:00">20:00</time>
  for at least
  <time datetime="PT2H30M">2h 30m</time>.
</p>
Try It Yourself

How It Works

  1. Time of day: HH:MM (24-hour clock).
  2. Duration: start with P, use T before hours/minutes — PT2H30M.
  3. Separate elements for start vs length keep each value clear.
  4. Labels can differ from the attribute as long as they mean the same thing.

Example 3 — When Text Was Changed

On <ins> / <del>, datetime timestamps the edit.

HTML
<p>"You're late!"</p>
<del datetime="2018-05-01">
  <p>"I apologize for the delay."</p>
</del>
<ins cite="/changelog" datetime="2018-05">
  <p>"A wizard is never late …"</p>
</ins>
Try It Yourself

How It Works

  1. del marks removed text; ins marks added text.
  2. datetime says when the change happened (date or month is fine).
  3. cite (optional) points to a document that explains the change.
  4. Browsers typically strike through del and underline ins.

Edge Cases & Pitfalls

Most issues come from invalid formats or mixing human text with machine rules.

format

Invalid strings lose the timestamp

If the value cannot be parsed, the element has no associated machine date. Stick to documented microsyntaxes.

time

Omitting datetime has rules

Without datetime, <time> must not have element children, and its text must be a valid date/time string.

history

Avoid pre-Gregorian dates

MDN notes <time> is not for dates before the Gregorian calendar — those calculations are unreliable in HTML.

js

Property is dateTime

In JavaScript the IDL name is camelCase with a capital T: element.dateTime, not datetime.

obsolete

Do not use pubdate

That attribute is obsolete. Publication timing belongs in datetime (and your own metadata if needed).

Browser Support

datetime is an HTML attribute for time, ins, and del in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

datetime

Widely available with the HTML5 time and modification elements. Desktop and mobile browsers parse the attribute; styling of time/ins/del is consistent enough for everyday use.

96% Global support (approx.)
Google Chrome 31+
Full support
Mozilla Firefox 22+
Full support
Apple Safari 7+
Full support
Microsoft Edge 12+
Full support
Opera 18+
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+ (partial)
Partial
datetime Widely supported

Bottom line: Safe to use for semantic dates today. Treat datetime as metadata — browsers do not auto-format the visible text from it.

Key Takeaways

  • Role: machine-readable date, time, or duration alongside human text.
  • Elements: <time>, <ins>, and <del>.
  • Formats: dates, times, datetimes with offsets, weeks/months, and P… durations.
  • JS: use element.dateTime (capital T). Skip obsolete pubdate.

One line: datetime is the precise value; the element text is what humans read.

Frequently Asked Questions

It stores a machine-readable date, time, or duration (usually ISO 8601) while the element’s visible text stays human-friendly. Search tools and scripts can read the precise value.
time, ins, and del. It is most common on time. On ins and del it records when content was inserted or deleted.
No. It is optional. If you omit it, the time element’s text content must itself be a valid date/time string and the element must not contain other elements.
Common forms include YYYY-MM-DD, HH:MM, YYYY-MM-DDTHH:MM with optional timezone (Z or ±HH:MM), YYYY-MM, YYYY-Www, and durations such as PT2H30M.
Use the dateTime property (capital T) on the element, for example timeEl.dateTime = "2018-07-07". You can also use setAttribute("datetime", value).
No. pubdate on time is obsolete. Use datetime for the machine-readable value instead.

Next: declare

Learn the (legacy) declare attribute used with object elements.

declare tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful