3

I was wondering if there was a way to covert the 24 hour clock of a datetime_select to a 12 hour clock?

lucapette
  • 20,564
  • 6
  • 65
  • 59
Brian
  • 5,951
  • 14
  • 53
  • 77

2 Answers2

13

Rails 3.1 has the am/pm option built in:

<%= f.datetime_select :starttime, :ampm => true, :start_year => 2011, :order => [:month, :day, :year] %>
Terri
  • 131
  • 1
  • 3
  • 3
    This needs to be made more apparent in the docs. I couldn't find this absolutely anywhere except here. Great answer. – Eric Jan 09 '12 at 21:36
1

It seems there's no predefined helper to do this particular job automatically. You've to extend and write your own helper for this functionality.

Also, this is a very old plugin, I am not sure this works or is still maintained, for similar thing.

I suggest you to go ahead and write your own helper to do the job as suggested in another SO question like this:

def am_pm_hour_select(field_name)
select_tag(field_name,options_for_select([
    ["1 AM", "01"],["2 AM", "02"],["3 AM", "03"],["4 AM", "04"],["5 AM", "05"],["6 AM", "06"],
    ["7 AM", "07"],["8 AM", "08"],["9 AM", "09"],["10 AM", "10"],["11 AM", "11"],["Noon", "12"],
    ["1 PM", "13"],["2 PM", "14"],["3 PM", "15"],["4 PM", "16"],["5 PM", "17"],["6 PM", "18"],
    ["7 PM", "19"],["8 PM", "20"],["9 PM", "21"],["10 PM", "22"],["11 PM", "23"],["Midnight", "0"]]))
end

And then in the view:

<%= am_pm_hour_select "eventtime[start(4i)]" %>  
Community
  • 1
  • 1
Syed Aslam
  • 8,707
  • 5
  • 40
  • 54