Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if current time is before 10:27pm

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.

like image 446
Onichan Avatar asked Oct 15 '25 23:10

Onichan


1 Answers

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
like image 158
shivam Avatar answered Oct 17 '25 14:10

shivam