HTML <time> Element
The <time> tag in HTML is used to represent a specific time or duration. It can be used to mark up dates, times, or durations in a machine-readable format.
How to Use the <time> Tag
To use the <time> tag, simply wrap the date, time, or duration in the tag like this:
<time datetime="2022-05-15T09:30:00-04:00">May 15, 2022 9:30 AM</time>
In this example, the datetime attribute is used to provide the machine-readable date and time in the format of YYYY-MM-DDTHH:MM:SS-ZZ:ZZ. The content of the tag is what will be displayed to users on the page.
Main Attributes of the <time> Tag
| Attribute | Description |
|---|---|
| datetime | Specifies the date and/or time in a machine-readable format |
| pubdate | Specifies that the date and time are the publication date of the content |
Examples of Using the <time> Tag
Here are some examples of how you can use the <time> tag:
<p>The event starts at <time datetime="2022-06-20T16:00:00-04:00">4:00 PM</time></p>
<article>
<h2>Article Title</h2>
<p>Published on <time datetime="2022-04-10" pubdate>April 10, 2022</time></p>
</article>
By using the <time> tag, you can provide additional context about dates and times within your HTML code.