检查当前时间是否在晚上10:27之前

Oni*_*han 3 ruby time ruby-on-rails

找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前10:27 pm

当前代码(仅比较小时,而不是分钟):

Time.now.getlocal("-05:00").hour.between?(10, 22)
Run Code Online (Sandbox Code Playgroud)

例如,我们的商店营业时间是9:24am - 10:27pm,我们想检查它是否当前是开放的.

shi*_*vam 7

正如文档中明确提到的那样:

你也可以做两次比较等标准功能.

我想你可以这样做:

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
Run Code Online (Sandbox Code Playgroud)