我如何输入或选择时间而不会导致喜欢这个=> " 2000-01-01 08:00:00"如果我选择08:00 AM,输出必须是' 08:00 AM'在数据库上......用户必须只输入时间...
new.html.rb
<%= form_for @search, html: {class: "pure-form"} do |s| %>
<%= s.time_select :search %>
<% end %>
schema.rb
create_table "searches", force: :cascade do |t|
t.time "search"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
答案 0 :(得分:0)
require 'date'
date = DateTime.parse("2000-01-01 08:00:00")
formatted_date = date.strftime('%I:%M%p')
答案 1 :(得分:0)
Ruby和Rails没有任何数据类型只能定义一天的时间分数。甚至Ruby中的Time
class也被定义为&#34;日期和时间的抽象&#34; 。因此,在您的rails代码中,您将始终看到时间作为默认日期的一部分(2000年1月1日)。
但是,由于您在数据库中使用time
数据类型列,因此日期时间的日期部分将在存储时丢弃,仅存储时间。
因此,我认为,整个问题只是需要在Rails代码中精神上忽略日期时间的日期部分。在视图层,以及DB应该是正确的。