Use Time Tag for Semantic HTML

CSSCSSBeginner
Practice Now

Introduction

In this lab, you will explore the semantic HTML5 <time> tag and learn how to effectively use it to represent dates and times in web documents. The lab focuses on understanding the <time> tag's key characteristics, such as providing machine-readable temporal data while maintaining human-readable text, and demonstrating its implementation in various HTML contexts.

Through a step-by-step approach, you will create an HTML file, add <time> tags with datetime attributes, and understand how this semantic element enhances web accessibility and provides structured information about dates and times. By the end of the lab, you will have practical experience in using the <time> tag to improve the semantic meaning of temporal data in web pages.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("CSS")) -.-> css/BasicConceptsGroup(["Basic Concepts"]) html(("HTML")) -.-> html/BasicStructureGroup(["Basic Structure"]) css(("CSS")) -.-> css/BasicStylingGroup(["Basic Styling"]) html(("HTML")) -.-> html/TextContentandFormattingGroup(["Text Content and Formatting"]) javascript(("JavaScript")) -.-> javascript/DOMManipulationGroup(["DOM Manipulation"]) css/BasicConceptsGroup -.-> css/selectors("Selectors") css/BasicConceptsGroup -.-> css/properties("Properties") html/BasicStructureGroup -.-> html/lang_decl("Language Declaration") css/BasicStylingGroup -.-> css/text_styling("Text Styling") html/TextContentandFormattingGroup -.-> html/text_head("Text and Headings") javascript/DOMManipulationGroup -.-> javascript/dom_select("DOM Selection") javascript/DOMManipulationGroup -.-> javascript/dom_manip("DOM Manipulation") javascript/DOMManipulationGroup -.-> javascript/dom_traverse("DOM Traversal") subgraph Lab Skills css/selectors -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} css/properties -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} html/lang_decl -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} css/text_styling -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} html/text_head -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} javascript/dom_select -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} javascript/dom_manip -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} javascript/dom_traverse -.-> lab-451085{{"Use Time Tag for Semantic HTML"}} end

Understand Time Tag Basics

In this step, you'll learn about the HTML5 <time> tag, a semantic element designed to represent dates and times in a machine-readable format. The <time> tag helps improve web accessibility and provides structured information about temporal data.

Key characteristics of the <time> tag include:

  • Represents a specific period in time
  • Can contain human-readable text
  • Supports a machine-readable datetime attribute
  • Enhances the semantic meaning of date and time information on web pages

Here's a basic example to illustrate the <time> tag:

<p>The conference is scheduled for <time>2023-09-15</time>.</p>

In this example, the <time> tag allows both human readers to see the date and machines to parse the exact date format.

Another example showing different ways to use the <time> tag:

<article>
  <h2>Lab Publication</h2>
  <p>Published on <time datetime="2023-06-20">June 20, 2023</time></p>
</article>

The datetime attribute provides a standardized machine-readable date format, while the visible text remains user-friendly.

Create HTML File with Time Tag

In this step, you'll create an HTML file that demonstrates the usage of the <time> tag. You'll use the WebIDE to create and edit an HTML document in the ~/project directory.

First, navigate to the project directory and create a new HTML file called event-schedule.html:

cd ~/project

Open the WebIDE and create a new file named event-schedule.html. Then, add the following HTML content:

touch event-schedule.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Event Schedule</title>
  </head>
  <body>
    <h1>Upcoming Tech Conference</h1>
    <article>
      <h2>Conference Details</h2>
      <p>The annual tech conference will be held on <time>2023-09-15</time>.</p>
    </article>
  </body>
</html>

Example output when viewed in a web browser:

Notes: Learn more about How to preview HTML files in the WebIDE.

HTML file with time tag example

This example demonstrates how to use the <time> tag within a paragraph to represent a specific date. The tag provides semantic meaning to the date, making it easier for browsers and assistive technologies to understand the temporal information.

Add Datetime Attribute to Time Tag

In this step, you'll learn how to enhance the <time> tag by adding the datetime attribute. The datetime attribute provides a machine-readable format for dates and times, making the content more accessible and semantically meaningful.

Open the event-schedule.html file in the WebIDE and modify the existing <time> tag to include the datetime attribute:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Event Schedule</title>
  </head>
  <body>
    <h1>Upcoming Tech Conference</h1>
    <article>
      <h2>Conference Details</h2>
      <p>
        The annual tech conference will be held on
        <time datetime="2023-09-15">September 15, 2023</time>.
      </p>

      <h3>Session Timing</h3>
      <p>Morning keynote starts at <time datetime="09:00">9:00 AM</time>.</p>
    </article>
  </body>
</html>

The datetime attribute offers several benefits:

  • Provides a standardized, machine-readable date/time format
  • Allows different display text from the actual date/time value
  • Supports various formats like full dates, times, and datetime combinations

Example formats for datetime attribute:

  • Full date: 2023-09-15
  • Time: 09:00
  • Datetime: 2023-09-15T09:00
  • With timezone: 2023-09-15T09:00Z

Example output when viewed in a web browser:

Upcoming Tech Conference

Conference Details
The annual tech conference will be held on September 15, 2023.

Session Timing
Morning keynote starts at 9:00 AM.

Explore Time Tag with Pubdate Attribute

In this step, you'll learn about the pubdate attribute, a special boolean attribute for the <time> tag that indicates the publication date of an article or blog post. Although it's now considered obsolete in HTML5, understanding its historical usage provides insights into semantic HTML.

Open the event-schedule.html file in the WebIDE and modify the content to include a blog post example with the pubdate attribute:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Tech Blog Post</title>
  </head>
  <body>
    <article>
      <h1>Latest Tech Insights</h1>
      <header>
        <h2>HTML5 Semantic Elements</h2>
        <p>
          Published on
          <time datetime="2023-06-20" pubdate>June 20, 2023</time>
        </p>
      </header>

      <p>In this article, we explore the semantic meaning of HTML5 tags...</p>

      <footer>
        <p>
          Updated on
          <time datetime="2023-06-22">June 22, 2023</time>
        </p>
      </footer>
    </article>
  </body>
</html>

Key points about the pubdate attribute:

  • It's a boolean attribute (no value required)
  • Indicates the publication date of an article
  • Deprecated in HTML5, but still supported by many browsers
  • Typically used within <article> tags

Example output when viewed in a web browser:

Latest Tech Insights

HTML5 Semantic Elements
Published on June 20, 2023

In this article, we explore the semantic meaning of HTML5 tags...

Updated on June 22, 2023

Note: While pubdate is now considered obsolete, it demonstrates the evolution of semantic HTML and the importance of providing machine-readable date information.

Verify Time Tag Implementation

In this final step, you'll review and validate the implementation of the <time> tag in your HTML document. You'll create a comprehensive example that demonstrates the various ways to use the <time> tag with different attributes and contexts.

Open the event-schedule.html file in the WebIDE and update it with a complete example:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Tech Event Showcase</title>
  </head>
  <body>
    <article>
      <h1>Web Development Conference</h1>

      <section>
        <h2>Event Details</h2>
        <p>
          Conference Date:
          <time datetime="2023-09-15">September 15, 2023</time>
        </p>
        <p>
          Start Time:
          <time datetime="09:00">9:00 AM</time>
        </p>
      </section>

      <section>
        <h2>Blog Post</h2>
        <article>
          <h3>HTML5 Semantic Elements</h3>
          <p>
            Published on
            <time datetime="2023-06-20" pubdate>June 20, 2023</time>
          </p>
          <p>
            Last Updated:
            <time datetime="2023-06-22">June 22, 2023</time>
          </p>
        </article>
      </section>
    </article>
  </body>
</html>

Key implementation checklist:

  • Multiple <time> tags with different contexts
  • Use of datetime attribute for machine-readable dates
  • Inclusion of human-readable date text
  • Demonstration of pubdate attribute
  • Semantic HTML structure with <article> and <section> tags

Example output when viewed in a web browser:

Web Development Conference

Event Details
Conference Date: September 15, 2023
Start Time: 9:00 AM

Blog Post
HTML5 Semantic Elements
Published on June 20, 2023
Last Updated: June 22, 2023

Summary

In this lab, participants learned about the HTML5 <time> tag, a semantic element designed to represent dates and times in a machine-readable format. The lab guided learners through understanding the tag's key characteristics, including its ability to contain human-readable text and support a machine-readable datetime attribute, which enhances web accessibility and provides structured temporal information.

Through practical exercises, participants created an HTML file demonstrating the <time> tag's implementation, exploring its usage with different attributes like datetime and pubdate. They practiced creating semantic markup that allows both human readers and machines to interpret date and time information effectively, reinforcing the importance of using semantic HTML elements in web development.