HTML - datetime Attribute



The 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 datetime attribute is used with the <del>, <ins>, and <time> tags. 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

Following is the syntax of the HTML datetime attribute −

<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

Example

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>

On running the above code, it will generate an output consisting of the text displayed on the webpage.

Example

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>

When we run the above code, it will generate an output consisting of the text displayed on the webpage.

Example

Let's look at the following example, where we are going to use the datetime attribute along with the time 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>
   <h3>'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>

On executing the above script, the output window will pop up displaying the text on the webpage along with date and time.

html_attributes_reference.htm
Advertisements