92

I am using HTML5 input element with type=time. The problem is it shows time with 12 hours but I want it to show it in 24 hours a day. How can I make it to 24 hours?

Here is my input field:

<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>
leonheess
  • 16,068
  • 14
  • 77
  • 112
Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • 3
    Open bug on Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=263320. Please vote :) – foal Jul 29 '17 at 17:27
  • 2
    Open bug on Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1425218. It indicates that: status-firefox59: --- → wontfix / status-firefox60: --- → affected / status-firefox61: --- → affected / status-firefox-esr52: --- → unaffected – Dennis Hendriks Aug 10 '18 at 08:57
  • @DennisHendriks and here is the solution mentioned in this bug, that solved the problem for me in Firefox: we need to set `intl.regional_prefs.use_os_locales` to true in about:config. This will follow operating system locale and also use a date like DD/MM/YYYY instead of MM/DD/YYYY depending on your settings. – baptx Apr 06 '20 at 11:45
  • @baptx Yes, I saw that. It works for me. – Dennis Hendriks Apr 07 '20 at 16:24
  • @DennisHendriks in fact on Linux, by using `LC_TIME=C.UTF-8` in /etc/default/locale, we don't even need to set `intl.regional_prefs.use_os_locales` to true. It may be related to this Thunderbird bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1426907#c155 – baptx Apr 08 '20 at 14:15

11 Answers11

70

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">
Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
30

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.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • 1
    In addition you can add a maxlength attribute and fix it to 5, you can also limit the size of the field to hint it. – Christophe Roussy Jun 22 '15 at 13:06
  • 1
    The link to the HTML5 draft is now broken, try https://www.w3.org/TR/html-markup/input.time.html instead. – dmiller309 Apr 08 '16 at 23:00
  • 3
    My Firefox browser for some reason displays time in 12 hours format, although Windows is set to use 24 hours. And i cannot find option to change to 24 hours format. --- Very weird decision not to allow page creator to set time format, if only them know for what region they page is created. – Andrei Aug 21 '19 at 06:59
13

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

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Alex
  • 155
  • 1
  • 2
  • 28
    I can not confirm this. Running Windows 10 that shows the time in 24 hours in the notification area, but I still get an AM/PM selector on my input type="time". Tested in Chrome and Edge. – Jan Aagaard Mar 16 '17 at 06:53
  • 5
    It depends on browser's locale, not systems'. – Episodex Mar 05 '19 at 09:42
12

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).

2540625
  • 11,022
  • 8
  • 52
  • 58
  • 21
    I don't so, my computer works under 24h format but my chrome works with pm/am format – Mehdi Souregi Aug 07 '18 at 10:22
  • 2
    Same here: on Linux, my desktop is configured to 24h time format, but Chrome insists on showing AM/PM. Super annoying as no one uses AM/PM here in Sweden. – Colin 't Hart Apr 26 '21 at 14:11
5

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>
Jonatas Walker
  • 13,583
  • 5
  • 53
  • 82
1

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.

Dima
  • 6,721
  • 4
  • 24
  • 43
1

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

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

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.

km1882
  • 740
  • 5
  • 22
0

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.

microsoftjulius
  • 300
  • 3
  • 10
-3

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>
-4

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;
 }
Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
Alex Kumbhani
  • 125
  • 2
  • 11