HTML input time in 24 format

Html

Html Problem Overview


I am using below HTML tag:

<input type="time"  />

In Chrome, Its showing PM/AM format, can we restrict to display 24 format always. I dont want to use HTML5 tag

Html Solutions


Solution 1 - Html

As stated in this answer not all browsers support the standard way. It is not a good idea to use for robust user experience. And if you use it, you cannot ask too much.

Instead use time-picker libraries. For example: TimePicker.js is a zero dependency and lightweight library. Use it like:

var timepicker = new TimePicker('time', {
  lang: 'en',
  theme: 'dark'
});
timepicker.on('change', function(evt) {
  
  var value = (evt.hour || '00') + ':' + (evt.minute || '00');
  evt.element.value = value;

});

<script src="https://cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.js"></script>
<link href="https://cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.css" rel="stylesheet"/>

<div>
  <input type="text" id="time" placeholder="Time">
</div>

More info about the time input can be found on MDN Web Docs

Solution 2 - Html

<!DOCTYPE html>
<html>
    <body>
        Time: <input type="time" id="myTime" value="16:32:55">

	<p>Click the button to get the time of the time field.</p>

	<button onclick="myFunction()">Try it</button>

	<p id="demo"></p>

	<script>
	    function myFunction() {
	        var x = document.getElementById("myTime").value;
	        document.getElementById("demo").innerHTML = x;
	    }
	</script>
    </body>
</html>

I found that by setting value field (not just what is given below) time input will be internally converted into the 24hr format.

Solution 3 - Html

You can't do it with the HTML5 input type. There are many libs available to do it, you can use momentjs or some other jQuery UI components for the best outcome.

Solution 4 - Html

In my case, it is taking time in AM and PM but sending data in 00-24 hours format to the server on form submit. and when use that DB data in its value then it will automatically select the appropriate AM or PM to edit form value.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVikas SinghView Question on Stackoverflow
Solution 1 - HtmlGokhan KurtView Answer on Stackoverflow
Solution 2 - HtmlVikas NSView Answer on Stackoverflow
Solution 3 - HtmlBill GauveyView Answer on Stackoverflow
Solution 4 - HtmlAbid_015View Answer on Stackoverflow