如何更改 < 输入 > 的时间格式(12/24小时) ?

我在 type=time中使用 HTML5输入元素。问题是它显示的时间与 12 hours,但我希望它显示在 24 hours的一天。我怎么才能坚持到24小时?

这是我的输入框:

<input type="time" name="time" placeholder="hrs:mins" pattern="^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$" class="inputs time" required>

293713 次浏览

HTML5 Time Input

This one 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 OS locale configuration. The value returned is in 24h hours:minutes format, which will look something like 14:30.

More details, including the appearance for each browser, can be found on MDN.

<input type="time" name="time">

Support of this type is still very poor. Opera shows it in a way you want. Chrome 23 shows it with seconds and AM/PM, in 24 version (dev branch at this moment) it will rid of seconds (if possible), but no information about AM/PM.
It's not want you possibly want, but at this point the only option I see to achieve your time picker format is usage of javascript.

By HTML5 drafts, input type=time creates a control for time of the day input, expected to be implemented using “the user’s preferred presentation”. But this really means using a widget where time presentation follows the rules of the browser’s locale. So independently of the language of the surrounding content, the presentation varies by the language of the browser, the language of the underlying operating system, or the system-wide locale settings (depending on browser). For example, using a Finnish-language version of Chrome, I see the widget as using the standard 24-hour clock. Your mileage will vary.

Thus, input type=time are based on an idea of localization that takes it all out of the hands of the page author. This is intentional; the problem has been raised in HTML5 discussions several times, with the same outcome: no change. (Except possibly added clarifications to the text, making this behavior described as intended.)

Note that pattern and placeholder attributes are not allowed in input type=time. And placeholder="hrs:mins", if it were implemented, would be potentially misleading. It’s quite possible that the user has to type 12.30 (with a period) and not 12:30, when the browser locale uses “.” as a separator in times.

My conclusion is that you should use input type=text, with pattern attribute and with some JavaScript that checks the input for correctness on browsers that do not support the pattern attribute natively.

Its depends on your locale system time settings, make 24 hours then it will show you 24 hours time.

Even though you see the time in HH:MM AM/PM format, on the backend it still works in 24 hour format, you can try using some basic javascript to see that.

HTML provide only input type="time" If you are using bootstrap then you can use timepicker. You can get code from this URL http://jdewit.github.io/bootstrap-timepicker May be it will help you

If you are able/willing to use a tiny component I wrote this Timepicker — https://github.com/jonataswalker/timepicker.js — for my own needs.

Usage is like this:

var timepicker = new TimePicker('time', {
lang: 'en',
theme: 'dark'
});


var input = document.getElementById('time');


timepicker.on('change', function(evt) {
  

var value = (evt.hour || '00') + ':' + (evt.minute || '00');
evt.element.value = value;


});
body {
font: 1.2em/1.3 sans-serif;
color: #222;
font-weight: 400;
padding: 5px;
margin: 0;
background: linear-gradient(#efefef, #999) fixed;
}
input {
padding: 5px 0;
font-size: 1.5em;
font-family: inherit;
width: 100px;
}
<script src="http://cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.js"></script>
<link href="http://cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.css" rel="stylesheet"/>


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

It depends on the time format of the user's operating system when the web browser was launched.

So:

  • If your computer's system prefs are set to use a 24-hour clock, the browser will render the <input type="time"> element as --:-- (time range: 00:00–23:59).
  • If you change your computer's syst prefs to use 12-hour, the output won't change until you quit and relaunch the browser. Then it will change to --:-- -- (time range: 12:00 AM – 11:59 PM).

And (as of this writing), browser support is only about 75% (caniuse). Yay: Edge, Chrome, Opera, Android. Boo: IE, Firefox, Safari).

you can try this for html side

<label for="appointment-time">Choose Time</label>
<input class="form-control" type="time" ng-model="time" ng-change="ChangeTime()" />
<label class="form-control" >\{\{displayTime}}</label>

JavaScript Side

function addMinutes(time/*"hh:mm"*/, minsToAdd/*"N"*/)
{
function z(n)
{
return (n<10? '0':'') + n;
}
var bits = time.split(':');
var mins = bits[0]*60 + (+bits[1]) + (+minsToAdd);
return z(mins%(24*60)/60 | 0) + ':' + z(mins%60);
}


$scope.ChangeTime=function()
{
var d = new Date($scope.time);
var hours=d.getHours();
var minutes=Math.round(d.getMinutes());
var ampm = hours >= 12 ? 'PM' : 'AM';
var Time=hours+':'+minutes;
var DisplayTime=addMinutes(Time, duration);
$scope.displayTime=Time+' - '+DisplayTime +' '+ampm;
}

Simple HTML trick to get this :

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row" >
                       

<div class="col-md-6">
<div class="row">
<div class="col-md-4" >
<label for="hours">Hours</label>
<select class="form-control" required>
<option>  </option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
<option value="5"> 5 </option>
<option value="6"> 6 </option>
<option value="7"> 7 </option>
<option value="8"> 8 </option>
<option value="9"> 9 </option>
<option value="10"> 10 </option>
<option value="11"> 11 </option>
<option value="12"> 12 </option>
</select>
</div>
<div class="col-md-4" >
<label for="minutes">Minutes</label>
<select class="form-control" required="">
<option selected disabled>  </option>
<option value="00"> 00 </option>
<option value="10"> 10 </option>
<option value="20"> 20 </option>
<option value="30"> 30 </option>
<option value="40"> 40 </option>
<option value="50"> 50 </option>
</select>
</div>
<div class="col-md-4" >
<label for="hours">Select Meridiem</label>
<select class="form-control" required="" >
<option selected="" value="AM"> AM </option>
<option value="PM"> PM </option>
</select>
</div>
</div></div>
</div>

If your computer's system prefs are set to use a 24-hour clock, the browser will render the element as --:-- (time range: 00:00–23:59). If you change your computer's syst prefs to use 12-hour, the output won't change until you quit and relaunch the browser.