👀 Live Preview
Article published with machine-readable datetime:
Article published on .

The datetime attribute is a powerful feature in HTML that associates a machine-readable date, time, or duration with an element. It is most commonly used on the <time> element to mark publication dates, event schedules, and other temporal information while keeping friendly text for readers.
Standard format.
Primary use.
Many formats.
P3W, PT1H.
JS property.
Revisions.
datetimeThe primary purpose of the datetime attribute is to convey machine-readable date and time information. Browsers, assistive technologies, scripts, and search engines can parse the standardized value while users see natural language in the element’s text content.
Write <time datetime="2024-06-15">June 15, 2024</time>. Readers see the friendly date; software reads 2024-06-15.
Add datetime to a <time>, <ins>, or <del> element:
<p>Published <time datetime="2024-01-22T12:30:00Z">January 22, 2024</time></p>
<p>Updated <ins datetime="2024-03-01">pricing section</ins></p>datetime is optional on <time>; when omitted, text content must be a valid date/time string.Z or +05:30) when precision matters.dateTime (capital T) on HTMLTimeElement.The datetime attribute accepts values in ISO 8601 format. Common patterns include:
YYYY-MM-DD (e.g. 2024-01-22 for January 22, 2024)HH:MM:SS (e.g. 14:30:00 for 2:30 PM)YYYY-MM-DDTHH:MM:SS (e.g. 2024-01-22T12:30:00)Z (e.g. 2024-01-22T12:30:00Z)2024-01-22T12:30:00+05:30P3W (3 weeks), PT2H30M (2 hours 30 minutes)Some docs show THH:MM:SS with a leading T. In HTML, a valid time string is typically HH:MM:SS or HH:MM. The T separator is required between date and time in combined values, not in standalone time-only values.
| Type | Example datetime value | Use case |
|---|---|---|
| Date | 2024-06-15 | Event day, blog date |
| Date + time | 2024-06-15T18:00:00 | Event start |
| UTC | 2024-06-15T18:00:00Z | Global timestamp |
| Time only | 14:30:00 | Daily schedule |
| Duration | P3W or PT1H30M | How long |
| JavaScript | el.dateTime = isoString | Dynamic updates |
| Element | Supported? | Notes |
|---|---|---|
<time> | Yes | Most common — dates, times, durations |
<ins> | Yes | Timestamp of inserted content |
<del> | Yes | Timestamp of deleted content |
<span>, <p> | No | Use <time> inside them instead |
| Part | Who reads it | Example |
|---|---|---|
| Element text | Human readers | January 22, 2024 at 12:30 PM |
| datetime attribute | Browsers, scripts, SEO | 2024-01-22T12:30:00Z |
| When they differ | Both valid | Friendly label + precise ISO value |
| When omitted | Text must parse | <time>2024-01-22</time> |
Publication date with time, and dynamic datetime via JavaScript.
Article published with machine-readable datetime:
Article published on .
The datetime attribute is often used with the <time> element:
<p>Article published on <time datetime="2024-01-22T12:30:00Z">January 22, 2024 at 12:30 PM UTC</time>.</p>In this example, datetime is set to a specific moment in ISO 8601 format with a UTC Z suffix, while the visible text explains the date in plain language.
You can dynamically set the datetime attribute to reflect the current date and time or respond to user interactions:
<time id="dynamicTime"></time>
<script>
var el = document.getElementById("dynamicTime");
var iso = new Date().toISOString();
el.dateTime = iso;
el.textContent = iso;
</script>In this script, dateTime is set on an element with id dynamicTime using new Date().toISOString(), which returns an ISO 8601 UTC string. Update visible text separately so users see a friendly format if desired.
<time>, not only a raw ISO string.<time> element identifying temporal content.datetime help assistive tech and calendar integrations parse dates reliably.datetime on ins and del to document when content changed.ISO 8601 value on time, ins, or del.
Visible content shows natural language dates.
Scripts, SEO, and calendars use the attribute value.
Accurate machine-readable timestamps with good UX.
The datetime attribute is supported in all modern browsers on <time>, <ins>, and <del> elements.
All major browsers honor datetime on supported elements.
Bottom line: Use datetime confidently with ISO 8601 values on <time> elements.
datetime attribute to ensure consistency and compatibility.datetime attribute with the <time> element when displaying dates and times for enhanced semantics.The datetime attribute is a valuable tool for associating precise date and time information with HTML elements. By pairing machine-readable ISO values with human-friendly text, you improve accessibility, semantics, and the interpretability of temporal data on your web pages.
Use it on <time> for articles and events, on <ins> and <del> for revision history, and update it dynamically with dateTime when you need live timestamps.
datetimeBookmark these before marking up dates and times.
Standard format.
ValuesPrimary use.
HTMLZ or offset.
UTCJS property.
ScriptFor humans.
UXtime, ins, and del. It is most commonly used on <time>.2024-01-22 for dates, 2024-01-22T12:30:00Z for UTC datetimes, and P3W for durations.element.dateTime = new Date().toISOString() on an HTMLTimeElement.Z means UTC (Coordinated Universal Time). Example: 2024-01-22T12:30:00Z.Practice the datetime attribute with publication dates, formats, and JavaScript in the Try It editor.
3 people found this page helpful