3

Found many ways to check if the current hour is within an hour range. But how would you check if the current time is before a minute-specific time like 10:27 pm

Current Code (only compares hours, not minutes):

Time.now.getlocal("-05:00").hour.between?(10, 22)

For example our store hours are 9:24am - 10:27pm and we want to check if it is currently open.

Onichan
  • 4,476
  • 6
  • 31
  • 60
  • If you want simple: `(hour*60)+minute < (hourGoal*60)+minuteGoal` (Here `minute`, `hour`, `minuteGoal`, and `hourGoal` aren't actual variable, you would replace them by the full call) – Patrick vD Nov 24 '15 at 03:38
  • Possible duplicate of [How to check given time is after 3pm or not in Rails?](http://stackoverflow.com/questions/6828294/how-to-check-given-time-is-after-3pm-or-not-in-rails) – Brad Werth Nov 24 '15 at 04:03

1 Answers1

8

As clearly mentioned in docs:

You can also do standard functions like compare two times.

I think you can do something like this:

require 'time'
close_time = Time.parse "10:27 pm"
#=> 2015-11-23 22:27:00 -0800
current_time = Time.now 
#=> 2015-11-23 19:38:06 -0800
current_time < close_time
#=> true # means the store is open

late_time = Time.now + 4*60*60
#=> 2015-11-23 23:39:15 -0800
late_time < close_time
#=> false # means the store is close
shivam
  • 16,048
  • 3
  • 56
  • 71