0

我有一个这样的表单输入代码:

 <%= f.time_select :delivery_time, {:default => 5.hours.from_now, :minute_step => 5, :ampm => true}, {:class=>"input-small"} %>

这是提交值,如:2013-04-09 23:00:00 UTC

但我想要像:23:00 这样的值,因为无论日期如何,都需要按时间定义条件。我尝试做类似的事情:

    if (@order.delivery_time.between?('21:00', '00:00')) 
      @order.total = @order.total + @@mnc 
    end 

但是它给出了错误,因为delivery_time 是nil:class。但是当打印 delivery_time 它打印:2013-04-09 23:00:00 UTC。

不知道如何让它工作。任何人都可以帮忙吗?

4

1 回答 1

1

您可能会想出一些日期值,以便您可以调用#between?. 仅提取您需要比较的日期部分会更简单,也更能揭示意图。

def late_order?(time)
  time.hour > 21
end

delivery_time = Time.new(2012, 1, 1, 22)
total = 300
total += 75 if late_order?(delivery_time)

puts "total for order: #{total}"   #=> 375
于 2013-04-09T22:33:00.580 回答