HTML datetime Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 3 Examples
Dates & Time

Introduction

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.

What You’ll Learn

01

ISO 8601

Standard format.

02

time Tag

Primary use.

03

Date / Time

Many formats.

04

Durations

P3W, PT1H.

05

dateTime

JS property.

06

ins / del

Revisions.

Purpose of datetime

The 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.

💡
Human text + machine value

Write <time datetime="2024-06-15">June 15, 2024</time>. Readers see the friendly date; software reads 2024-06-15.

📝 Syntax

Add datetime to a <time>, <ins>, or <del> element:

datetime.html
<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>

Syntax Rules

  • Use ISO 8601 formats for consistency and broad compatibility.
  • datetime is optional on <time>; when omitted, text content must be a valid date/time string.
  • Include timezone info (Z or +05:30) when precision matters.
  • JavaScript property is dateTime (capital T) on HTMLTimeElement.

💎 Values

The datetime attribute accepts values in ISO 8601 format. Common patterns include:

  • Date-onlyYYYY-MM-DD (e.g. 2024-01-22 for January 22, 2024)
  • Time-onlyHH:MM:SS (e.g. 14:30:00 for 2:30 PM)
  • Combined date and timeYYYY-MM-DDTHH:MM:SS (e.g. 2024-01-22T12:30:00)
  • UTC timezone — append Z (e.g. 2024-01-22T12:30:00Z)
  • Offset timezone2024-01-22T12:30:00+05:30
  • DurationP3W (3 weeks), PT2H30M (2 hours 30 minutes)
⚠️
Time-only prefix

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.

⚡ Quick Reference

TypeExample datetime valueUse case
Date2024-06-15Event day, blog date
Date + time2024-06-15T18:00:00Event start
UTC2024-06-15T18:00:00ZGlobal timestamp
Time only14:30:00Daily schedule
DurationP3W or PT1H30MHow long
JavaScriptel.dateTime = isoStringDynamic updates

Applicable Elements

ElementSupported?Notes
<time>YesMost common — dates, times, durations
<ins>YesTimestamp of inserted content
<del>YesTimestamp of deleted content
<span>, <p>NoUse <time> inside them instead

datetime vs visible text

PartWho reads itExample
Element textHuman readersJanuary 22, 2024 at 12:30 PM
datetime attributeBrowsers, scripts, SEO2024-01-22T12:30:00Z
When they differBoth validFriendly label + precise ISO value
When omittedText must parse<time>2024-01-22</time>

Examples Gallery

Publication date with time, and dynamic datetime via JavaScript.

👀 Live Preview

Article published with machine-readable datetime:

Article published on .

Example

The datetime attribute is often used with the <time> element:

datetime.html
<p>Article published on <time datetime="2024-01-22T12:30:00Z">January 22, 2024 at 12:30 PM UTC</time>.</p>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

You can dynamically set the datetime attribute to reflect the current date and time or respond to user interactions:

dynamic-datetime.html
<time id="dynamicTime"></time>



<script>

  var el = document.getElementById("dynamicTime");

  var iso = new Date().toISOString();

  el.dateTime = iso;

  el.textContent = iso;

</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Readable text — Always provide human-friendly content inside <time>, not only a raw ISO string.
  • Semantic markup — Screen readers benefit from the <time> element identifying temporal content.
  • Timezone clarity — Mention timezone in visible text when it matters to users in different regions.
  • Consistent formats — ISO values in datetime help assistive tech and calendar integrations parse dates reliably.
  • Revision timestamps — Use datetime on ins and del to document when content changed.

🧠 How datetime Works

1

Author adds datetime

ISO 8601 value on time, ins, or del.

Markup
2

Users read friendly text

Visible content shows natural language dates.

Display
3

Software parses datetime

Scripts, SEO, and calendars use the attribute value.

Parse
=

Semantic temporal data

Accurate machine-readable timestamps with good UX.

Browser Support

The datetime attribute is supported in all modern browsers on <time>, <ins>, and <del> elements.

HTML5 · Fully supported

Universal datetime attribute

All major browsers honor datetime on supported elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
datetime attribute 99% supported

Bottom line: Use datetime confidently with ISO 8601 values on <time> elements.

💡 Best Practices

✅ Do

  • Use ISO 8601 formats for datetime values
  • Pair datetime with time for publication dates
  • Include timezone (Z or offset) when precision matters
  • Write friendly visible text for readers
  • Use dateTime in JS for live timestamps

❌ Don’t

  • Use ambiguous date formats like 01/02/2024 in datetime
  • Put datetime on non-supported elements
  • Show only raw ISO strings without readable text
  • Forget timezone when scheduling global events
  • Confuse datetime with the data or data-* attributes
  • Always use the ISO 8601 format when providing values for the datetime attribute to ensure consistency and compatibility.
  • Incorporate the datetime attribute with the <time> element when displaying dates and times for enhanced semantics.
  • When using JavaScript to set dynamic values, be mindful of time zones to provide accurate temporal information.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about datetime

Bookmark these before marking up dates and times.

5
Core concepts
02

time Element

Primary use.

HTML
🌐 03

Timezone

Z or offset.

UTC
⚙️ 04

dateTime

JS property.

Script
📝 05

Readable Text

For humans.

UX

❓ Frequently Asked Questions

It stores a machine-readable date, time, or duration in ISO 8601 format while the element shows human-friendly text.
time, ins, and del. It is most commonly used on <time>.
Use ISO 8601: 2024-01-22 for dates, 2024-01-22T12:30:00Z for UTC datetimes, and P3W for durations.
No, but recommended when visible text is not already a valid parseable date/time string.
Use element.dateTime = new Date().toISOString() on an HTMLTimeElement.
Z means UTC (Coordinated Universal Time). Example: 2024-01-22T12:30:00Z.

Mark up dates the semantic way

Practice the datetime attribute with publication dates, formats, and JavaScript in the Try It editor.

Try publication date example →

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.

3 people found this page helpful