我正在尝试将日期和时间字段从表单保存并转换为单个日期时间属性。目前我按照以下方式通过日期和时间
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4vsUi3DN9ucsxkrojPMe/J4tvJwsIebvGzv0gC0/w8g=", "event"=>{"team_id"=>"4", "date"=>"Monday, July 01, 2013", "time"=>"06:15 am", "event"=>"1", "location_name"=>"Another Loc", "duration"=>"10", "arrival_time"=>"10", "opponent_name"=>"Test", "home_or_away"=>"Home"}, "commit"=>"Create Event"}
并尝试使用以下
保存此数据params[:event][:datetime] = DateTime.parse(params[:event][:date]+params[:event][:time])
@event = Event.new(params[:event])
这似乎正在正确地保存数据,但是当我尝试使用以下内容显示时,我将获得未来10小时的时间
Time::DATE_FORMATS[:event_time] = '%l:%M %p'
答案 0 :(得分:-1)
time_to_merge = Time.new
date_to_merge = Date.today
merged_datetime = DateTime.new(date_to_merge.year, date_to_merge.month,
date_to_merge.day, time_to_merge.hour,
time_to_merge.min, time_to_merge.sec)