Ruby on Rails datetime_select

时间:2011-06-15 06:14:11

标签: ruby-on-rails

我想知道是否有办法将datetime_select的24小时制转换为12小时制?

2 个答案:

答案 0 :(得分:13)

Rails 3.1内置了am / pm选项:

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

答案 1 :(得分:1)

似乎有no predefined helper to do this particular job automatically。您需要extend and write自己的帮助程序才能使用此功能。

此外,this is a very old plugin,我不确定这是否有效或仍然保持,类似的事情。

我建议你继续按照another SO question中的建议编写自己的帮助来完成这项工作:

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

然后在视图中:

<%= am_pm_hour_select "eventtime[start(4i)]" %>