如何用Ruby on Rails判断当前时间是否在晚上10:27之前?

更新于
2026-10-09 18:44:51
2阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计307个文字,预计阅读时间需要2分钟。

如何用Ruby on Rails判断当前时间是否在晚上10:27之前?

找到检查当前小时间隔内的方法。但要确定当前时间是否在特定时间之前,如下午10:27,可以直接使用以下代码比较小时(不比较分钟):

rubycurrent_time=Time.now.getlocal(05:00)time_before=Time.new(current_time.year, current_time.month, current_time.day, 10, 27)time_now=Time.now.getlocal(05:00)

is_before=current_time.hour 找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前,如下午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.

如何用Ruby on Rails判断当前时间是否在晚上10:27之前?

我想你可以这样做:

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

本文共计307个文字,预计阅读时间需要2分钟。

如何用Ruby on Rails判断当前时间是否在晚上10:27之前?

找到检查当前小时间隔内的方法。但要确定当前时间是否在特定时间之前,如下午10:27,可以直接使用以下代码比较小时(不比较分钟):

rubycurrent_time=Time.now.getlocal(05:00)time_before=Time.new(current_time.year, current_time.month, current_time.day, 10, 27)time_now=Time.now.getlocal(05:00)

is_before=current_time.hour 找到了许多方法来检查当前小时是否在一小时范围内.但是你如何检查当前时间是否在特定时间之前,如下午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.

如何用Ruby on Rails判断当前时间是否在晚上10:27之前?

我想你可以这样做:

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