Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f.time_field with 24 hour clock - Rails Fields

I've got a little Rails app that I need to use the 24-hour clock for inputting times. Right now, I've got:

  <div class=".col-md-7">
    <%= f.label :backinservice %><br>
    <%= f.time_field :backinservice %>
  </div>

Which gives an AM/PM option. When I input a time like 18:56, it automagically converts it to 06:56 PM. Normally that would be totally ok, just not in this app.

I also tried:

  <div class=".col-md-7">
    <%= f.label :recieved %><br>
    <%= f.time_field :recieved, :format=>"%H:%M" %>
  </div>

But that doesn't work either.

Is there a :format option that allows for straight use of the 24-hour clock?

like image 615
PSCampbell Avatar asked Oct 19 '25 21:10

PSCampbell


1 Answers

You have to pass the format using value key.

<div class=".col-md-7">
  <%= f.label :recieved %><br>
  <%=
    f.time_field :recieved,
      value: "%H:%M",
      min: 'hh:mm:ss',
      max: 'hh:mm:ss'
  %>
</div>

The doc says:

The default value is generated by trying to call strftime with “%T.%L” on the objects's value. It is still possible to override that by passing the “value” option.

like image 66
Arup Rakshit Avatar answered Oct 21 '25 20:10

Arup Rakshit