加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 检查当前时间是否在晚上10:27之前

发布时间:2020-12-17 01:35:40 所属栏目:百科 来源:网络整理
导读:找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前,如下午10:27 当前代码(仅比较小时,而不是分钟): Time.now.getlocal("-05:00").hour.between?(10,22) 例如,我们的营业时间是上午9:24 – 晚上10:27,我们想检查它
找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前,如下午10:27

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

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

例如,我们的营业时间是上午9:24 – 晚上10:27,我们想检查它是否当前是打开的.

解决方法

正如 docs中明确提到的:

You can also do standard functions like compare two times.

我想你可以这样做:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读