HTML - datetime Attribute



HTML datetime attribute is used to indicate the date and time associated with the element.

It is used to specify the date and time when the text has been inserted or deleted. The value of the datetime attribute must be in a valid format with an optional time string. If the value can't be parsed as a date with an optional time string, the element is not having an associated timestamp.

Syntax

<tag datetime = "value"></tag>

Where value can be any date and time as the following format −

Year-month-day or 2023-07-11 or 2023-07-11T15:17:00

Applies On

Below listed elements allow using of the HTML datetime attribute

Element Description
<del> HTML <del> tag is use to mark the deleted textual content on the website.
<ins> HTML <ins> tag represents a range of text that has been inserted into an HTML document.
<time> HTML <time> tag is used to define a specific date and time.

Examples of HTML datetime attribute

Bellow examples will illustrate the HTML datetime attribute, where and how we should use this attribute!

Define deleted element datetime

In the following example, we are using the HTML ‘datetime’ attribute within the <del> tag to indicate the date and time associated with this element.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'datetime' attribute</title>
</head>
<body>
   <!--HTML 'datetime' attribute-->
   <p>Example of the HTML 'datetime' attribute</p>
   <del datetime="2023-07-11T14:26:00">
      Text within the 'del' tag has been deleted.
   </del>
</body>
</html>

Define inserted element datetime

Considering the another scenario, where we are going to use the datetime attribute along with the ins tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'datetime' attribute</title>
</head>
<body>
   <!--HTML 'datetime' attribute-->
   <p>Example of the HTML 'datetime' attribute</p>
   <ins datetime="2023-07-11T14:42:04">
      Text inserted within the 'ins' element.
   </ins>
</body>
</html>

Datetime attribute with time tag

Let's look at the following example, where we are going to use the datetime attribute along with the time tag and use javascript to print current time. This is used for machin readable.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'datetime' attribute</title>
</head>
<body>
   <!--HTML 'datetime' attribute-->
   <p>Example of the HTML 'datetime' attribute</p>
   <h3>HTML 'datetime' attribute with 'time' element</h3>
   <p>The concert is on 
      <time datetime="2023-07-15T15:02:00">
   15 July</time>
   </p>
   <p>Current time is: 
      <time datetime="2023-07-11T15:03:00" id='demo'></time>
   </p>
   <script>
      var d = new Date();
      var h = d.getHours();
      var m = d.getMinutes();
      var s = d.getSeconds();
      var res = document.getElementById('demo');
      res.innerHTML = h + " : " + m + " : " + s;
   </script>
</body>
</html>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
datetime Yes 62.0 Yes 18.0 Yes 22.0 Yes 7.0 Yes 49.0
html_attributes_reference.htm
Advertisements