Time select form helper with 12 hour format for Rails 3?

swese44 picture swese44 · Mar 24, 2011 · Viewed 10.9k times · Source

Is there a user-friendly time_select for Rails 3? The default time_select form helper gives you hours[00-23], minutes[00-59] and optionally seconds[00-59]. A dropdown list of hours 0-23 is pretty frustrating for those of us who aren't on military time. A user-friendly solution would display hours 1-12 and an extra am/pm dropdown list.

Is there an option or a separate plugin to handle this? I can't be the first person to want this, but I haven't found any solutions.

Thanks!

Answer

arturtr picture arturtr · Aug 25, 2012

You can set :ampm option to true which will show the hours as: 12 PM, 01 AM .. 11 PM.

time_select 'game', 'game_time', {:ampm => true}

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select