ruby-on-rails – Ruby on Rails datetime_select

我想知道是否有办法将datetime_select的24小时制转换为12小时制?
最佳答案
似乎有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)]" %>  

转载注明原文:ruby-on-rails – Ruby on Rails datetime_select - 代码日志