input type time in HTML

Rumman Ansari   Software Engineer   2024-01-12 06:19:54   490  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

HTML <input type="time">

HTML

<form action="/submit" method="post">
  <label for="appt">Select a time:</label>
  <input type="time" id="appt" name="appt">
  <input type="submit" value="Submit">
</form>
  1. <label for="appt">Select a time:</label>: This is a label element that provides a textual description for the associated input element. The for attribute is set to "appt," which corresponds to the id attribute of the associated input element. This association helps screen readers and improves accessibility.

  2. <input type="time" id="appt" name="appt">: This is the input element used for selecting a time. Here's a breakdown of its attributes:

    • type="time": Specifies that this input field is intended for entering a time. This triggers the browser to provide a time-specific interface for easier input.

    • id="appt": Assigns a unique identifier to the input element. The for attribute in the label is associated with this ID, creating a connection between the label and the input field.

    • name="appt": Provides a name for the input field, which is useful when submitting the form to the server. The name attribute allows you to reference the input field in server-side scripts.

When a user interacts with this input field, a time picker interface is displayed, allowing them to choose the desired time. The selected time will be submitted along with other form data when the form is submitted.

<time datetime="20025-02-14 20:00">Valentines day</time>


<p>Open from <time>10:00</time> to <time>21:00</time> every weekday.</p>

  1. <p>: This is a paragraph element, used to structure and separate content on the web page.

  2. Open from: This is plain text indicating the start of a statement about the opening hours.

  3. <time>10:00</time>: This is a <time> element representing the opening time, 10:00. The <time> element in HTML is used to represent specific points in time. In this case, it's indicating the opening time for a business or venue.

  4. to: This is plain text indicating the transition from the opening time to the closing time.

  5. <time>21:00</time>: Another <time> element representing the closing time, 21:00.

  6. every weekday: This is plain text specifying that the mentioned opening hours apply to every weekday.

The overall paragraph communicates the opening hours of a business or venue, specifying that it is open from 10:00 to 21:00 on every weekday.

Now, let's look at the second paragraph:


<p>I have a date on <time datetime="2025-02-14 20:00">Valentine's day</time>.</p>

  1. <p>: Again, this is a paragraph element.

  2. I have a date on: Plain text introducing a statement about a specific date.

  3. <time datetime="2025-02-14 20:00">: Another <time> element, but this time with a datetime attribute. The datetime attribute provides a machine-readable version of the date and time. In this case, it represents February 14, 2025, at 8:00 PM.

  4. Valentine's day: Plain text providing additional context, indicating that the mentioned date is Valentine's day.

The second paragraph communicates a personal event, stating that the author has a date on Valentine's day at 8:00 PM.

In both cases, the use of the <time> element adds semantic meaning to the HTML, making it more machine-readable and providing clear information about specific points in time.



Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.