Ruby on Rails如何判断现在时间是否在晚上10点27分之前?
- 内容介绍
- 文章标签
- 相关推荐
本文共计377个文字,预计阅读时间需要2分钟。
要检查当前时间是否在特定的小时范围内,可以使用以下方法:
ruby假设当前时间是下午10:27current_time=Time.now.getlocal(GMT)
检查当前小时是否在10到17之间(不包含17)is_within_range=current_time.hour.between?(10, 17)
输出结果puts is_within_range ? 当前时间在指定范围内 : 当前时间不在指定范围内
这段代码首先获取当前本地时间,然后使用`between?`方法检查当前小时是否在10到17之间(不包含17)。如果条件满足,`is_within_range`将变为`true`,否则为`false`。最后,根据结果输出相应的信息。
找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前,如下午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
本文共计377个文字,预计阅读时间需要2分钟。
要检查当前时间是否在特定的小时范围内,可以使用以下方法:
ruby假设当前时间是下午10:27current_time=Time.now.getlocal(GMT)
检查当前小时是否在10到17之间(不包含17)is_within_range=current_time.hour.between?(10, 17)
输出结果puts is_within_range ? 当前时间在指定范围内 : 当前时间不在指定范围内
这段代码首先获取当前本地时间,然后使用`between?`方法检查当前小时是否在10到17之间(不包含17)。如果条件满足,`is_within_range`将变为`true`,否则为`false`。最后,根据结果输出相应的信息。
找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前,如下午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

