HTML <time> Tag: Definition, Usage, and Examples
<time> tag in HTML is used to represent dates, times, or durations in a machine-readable format. It helps browsers and tools understand and process time-related data on a webpage.How It Works
The <time> tag acts like a label for dates and times on a webpage. Imagine you have a calendar or clock in your room; the <time> tag is like putting a clear sticker on the date or time so that both people and machines can easily recognize it.
Inside the tag, you can write a human-friendly date or time, and use the datetime attribute to provide a standard format that computers understand. This helps search engines, browsers, and assistive technologies know exactly what the date or time means, even if the visible text is written in a casual way.
Example
This example shows how to mark a date using the <time> tag with the datetime attribute:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Time Tag Example</title> </head> <body> <p>Event date: <time datetime="2024-06-15">June 15, 2024</time></p> </body> </html>
When to Use
Use the <time> tag whenever you want to mark up dates, times, or durations on your webpage. This is especially helpful for events, deadlines, blog post dates, or schedules.
For example, if you have a blog, marking the post date with <time> helps search engines show the date in search results. It also helps screen readers announce the date clearly to users who rely on them.
Key Points
- The
<time>tag makes dates and times understandable to machines. - Use the
datetimeattribute to provide a standard format (ISO 8601). - It improves accessibility and SEO by clarifying time-related information.
- The visible text inside can be human-friendly and different from the machine-readable format.