How to adjust the time format of the <input> element (12-hour or 24-hour)?
P粉488464731
P粉488464731 2023-08-20 18:47:36
0
2
729
<p>I'm using an HTML5 input element with <code>type=time</code>. The problem is that it displays the time in <code>12 hour format</code>, but I want it to display in <code>24 hour format</code>. How do I set it to 24 hour time? </p> <p>This is my input field: </p> <p><br /></p> <pre class="brush:html;toolbar:false;"><input type="time" name="time" placeholder="hour:minute" pattern="^([0-1]?[0 -9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$" class="inputs time" required>< /pre> <p><br /></p>
P粉488464731
P粉488464731

reply all(2)
P粉547170972

According to the HTML5 draft, input type=time creates a control for entering the time of day, which is expected to be implemented using the "user's preferred display method". But in practice, this means using a widget that follows the browser's localization rules. Therefore, regardless of the language of the surrounding content, the display will change based on the language of the browser, the language of the underlying operating system, or the system-wide locale (depending on the browser). For example, using the Finnish version of Chrome, the widgets I see use the standard 24-hour clock. Your situation may vary.

Therefore, input type=time is based on an approach that leaves the idea of ​​localization entirely outside of the page author. This is intentional; this question has been raised a few times in HTML5 discussions, with no change (except perhaps a clarification of the text describing this behavior as expected).

Please note that input type=time does not allow the pattern and placeholder attributes. Also, it can be misleading if placeholder="hrs:mins" is implemented. When the browser's locale uses "." as the time separator, the user may need to enter 12.30 (using a period) instead of 12:30.

My conclusion is that on browsers that don't support the pattern attribute, one should use input type=text and use some JavaScript to check the correctness of the input.

P粉231112437

HTML5 time input

This is the simplest of the date/time related types, allowing the user to select a time on a 24/12 hour clock, usually depending on the user's operating system locale. The value returned is in 24-hour hour:minute format and looks similar to 14:30.

More details, including how it looks in each browser, can be found on MDN.

<input type="time" name="time">
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template