📅  最后修改于: 2023-12-03 15:31:15.969000             🧑  作者: Mango
HTML provides various date and time attributes to help developers display and input dates and times easily. In this article, we will explore the commonly used date and time attributes in HTML and how to use them in your code.
The date
attribute is used to display a date in an HTML element. It can be used in combination with various elements such as p
, span
, h1
, h2
, etc.
<p>Date: <time date="2022-02-21">February 21, 2022</time></p>
The datetime
attribute is used to specify a date and time for the element to be used in machine-readable formats. It follows the format specified in RFC3339.
<p>Date: <time datetime="2022-02-21T12:00:00Z">February 21, 2022</time></p>
The time
attribute is used to display time in an HTML element. It supports a 24-hour format in the format HH:MM[:SS]
.
<p>Time: <time time="12:30">12:30 PM</time></p>
The datetime-local
attribute is used to create a date and time picker for the user to input a date and time. It supports a 24-hour format.
<label for="meeting-time">Choose a meeting time:</label>
<input type="datetime-local" id="meeting-time" name="meeting-time">
With the help of these date and time attributes, developers can easily display and input dates and times in their HTML code. It is important to use the correct format when using these attributes to ensure machine-readability and user-friendliness.