datetime

The datetime attribute in HTML defines a date and time in a machine-readable format for elements like <time>, improving recognition by browsers and search engines.

datetime attribute

The datetime attribute in HTML specifies a precise date and time for elements like <time>. It offers a machine-readable format, enabling browsers and search engines to accurately interpret and process temporal information within content.

Syntax

index.html
<tagname datetime="YYYY-MM-DDThh:mm:ssTZD" >

Example

Here’s an example of using the datetime attribute in HTML:

index.html
<time datetime="2024-09-22T14:30">
  September 22, 2024, 2:30 PM
</time>
  • In this example, the datetime attribute offers a machine-readable format for the given date and time, while the text inside shows a version that's readable for humans.

Values for the datetime Attribute

The format for the datetime attribute is YYYY-MM-DDThh:mm:ssTZD, which can be broken down as follows:

  • YYYY: Four-digit year (e.g., 2024)
  • MM: Two-digit month (e.g., 03 for March)
  • DD: Two-digit day of the month (e.g., 29)
  • T: Separator between the date and time (required if time is included)
  • hh: Hour in 24-hour format (e.g., 12 for noon)
  • mm: Two-digit minutes (e.g., 00)
  • ss: Two-digit seconds (e.g., 00)
  • TZD (Optional): Time zone designator
  • Z: Indicates Zulu Time (GMT/UTC)
  • Offsets can also be specified, such as "+07:00" for 7 hours ahead of UTC.

Applies To

The datetime attribute can be applied to the following HTML elements.

Example

This article was last updated on .

index.html
<p>This article was last updated on <time datetime="2024-03-29T11:58:00Z">Friday, March 29, 2024</time>.</p>

Conclusion

The datetime attribute in HTML provides a machine-readable format for dates and times, improving interpretation by browsers and search engines. It is primarily used with elements like <time>, <del>, and <ins>. By using this attribute, you ensure better accuracy and consistency in representing temporal information.