0

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?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user2206724
  • 1,265
  • 3
  • 20
  • 38
  • possible duplicate of [Rails: if condition for time range](http://stackoverflow.com/questions/15906657/rails-if-condition-for-time-range) – Dave Newton Apr 09 '13 at 19:03
  • @DaveNewton can u tell solution of issue? – user2206724 Apr 09 '13 at 19:08
  • [Pass something to `between?` it can actually use?](http://apidock.com/rails/ActiveSupport/TimeWithZone/between%3F) I don't know why your `delivery_time` is nil, I somewhat doubt it is since you can print it (you say). It could be how you build the object, I have no idea. – Dave Newton Apr 09 '13 at 19:09
  • hmmm. In database it enters value, 23:00, and in print it gives like 2013-04-09 23:00:00 UTC. As per API, tried :ignore_date, but gives multiassignment error. Not getting how can i compare delivery_time falls in given range or no. – user2206724 Apr 09 '13 at 19:16

1 Answers1

1

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
slothbear
  • 2,016
  • 3
  • 21
  • 38