Time format while submitting

54 Views Asked by At

I have a form input code like this:

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

This is submitting value like: 2013-04-09 23:00:00 UTC

But i want value like : 23:00 as need define condition on time irrespective of date. I tried doing something like:

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

But It gives error as delivery_time is nil:class. But when print delivery_time it prints : 2013-04-09 23:00:00 UTC.

Not getting how to get it work. Can anyone help?

1

There are 1 best solutions below

0
On

You could probably conjure some date values so that you could call #between?. It would be simpler and more intention-revealing to extract just the part of the date you need to compare.

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