合并DateTime字段的组件

时间:2012-04-10 21:29:34

标签: ruby-on-rails ruby-on-rails-3 datetime

我有两个属性start_timeend_time

如何将start_time输入的日期(不包括时间)用于end_time字段(因为开始+结束时间将在同一天发生,因此没有任何一点提示用户输入两次)。

查看:

<div class="day">
    <div class="field">
       <%= f.label :start_time, "Date"  %>
       <%= f.date_select :start_time,:default => Time.now+1.week, :order => [:month, :day, :year], :ampm => true %>
    </div>
    <div class="field">
        <%= f.label :start_time, "Start Time" %>
        <%= f.time_select :start_time, :ignore_date => true, :minute_step => 15, :ampm => true, :default => Time.now.beginning_of_day + 9.hours %>
    </div>
    <div class="field">
        <%= f.label :end_time, "End Time" %>
        <%= f.time_select :end_time, :minute_step => 15, :ampm => true, :default => Time.now.beginning_of_day + 15.hours %>
    </div>
    <%= link_to_function "Remove this day", "remove_fields(this)" %>
</div>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

实际上并没有这么简洁的方法。您将不得不提取单个组件:

t1 = start_time
t2 = end_time
t2 = Time.local(t1.year,t1.month,t1.day,t2.hour,t2.min,t2.sec).in_time_zone

请注意您所在的时区。您可能需要使用Time.utc代替Time.local