Brian
Brian

Reputation: 6071

Ruby on Rails datetime_select

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

Upvotes: 3

Views: 2704

Answers (2)

Terri
Terri

Reputation: 131

Rails 3.1 has the am/pm option built in:

<%= f.datetime_select :starttime, :ampm => true, :start_year => 2011, :order => [:month, :day, :year] %>

Upvotes: 13

Syed Aslam
Syed Aslam

Reputation: 8807

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)]" %>  

Upvotes: 1

Related Questions