How to set the date in HTML: Date input box: <input type="date">, allowing the user to directly enter the date. Calendar pop-up box: , the user can click on the pop-up calendar to select a date. JavaScript: Use the Date() object to dynamically get and set dates.
How to set the date in HTML
In HTML, you can use the following methods to set the date:
1. Date input box
<input type="date">
element is used to create a date input box, allowing users to directly enter dates.
Example:
<code class="html"><label for="dob">出生日期:</label> <input type="date" id="dob" name="dob"></code>
2. Calendar pop-up box
<input type="datetime-local The ">
element creates a date and time input box, and the user can click on the pop-up calendar to select a date.
Example:
<code class="html"><label for="appt">预约时间:</label> <input type="datetime-local" id="appt" name="appt"></code>
3. JavaScript
You can use JavaScript to dynamically set the date in an HTML element. For example, you can use a Date()
object to get the current date and assign it to the element's value
attribute.
Example:
<code class="javascript">const today = new Date(); const dateElement = document.getElementById("date"); dateElement.value = today.toLocaleDateString();</code>
The above are three common ways to set dates in HTML. Which method you choose depends on your specific needs and your website's user experience.
The above is the detailed content of How to set date in html. For more information, please follow other related articles on the PHP Chinese website!