提交时的时间格式

时间:2013-04-09 18:39:48

标签: ruby-on-rails-3 if-statement time-select

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

 <%= 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。

没有得到如何使它工作。有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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