2016-08-01 92 views
0

我对Ruby很新,我不明白How can i check whether the current time in between tonight 9pm and 9am(tomorrow) in Ruby on Rails的答案。如何检查时间是昨天上午9点到今天上午9点之间的Ruby?

我有一段时间,我想看看它是否在上午9点到上午9点之间 - 我该怎么做?

我使用的答案试过,但我不知道如果我做到了法正确

updated_at = #some time that I access 

if (0..8).cover? updated_at.hour 
    a = updated_at - 1.day 
else 
    a = updated_at 
end 

start = Time.new updated_at.year, updated_at.month, updated_at.day, 9, 0, 0 
b = a + 1.day 
stop = Time.new b.year, b.month, b.day, 9, 0, 0 

puts (start..stop).cover? updated_at 
+0

您链接到答案页你的问题并没有告诉我们那些你不明白的答案,所以我不确定你期望我们在这里做什么。 –

+0

所以我尝试使用该答案,但我不确定是否将它正确地重新使用 – praks5432

回答

0
yesterday_at_nine = Time.parse('9:00') - 1.day 
today_at_nine = Time.parse('9:00') 
now = Time.now 

(yesterday_at_nine..today_at_nine).cover? now 
0

这将确定当前时间是晚上9点在上午9时和之间:

​​

例如:

time = Time.now 
#=> 2016-08-01 20:23:32 +0000 
time.hour > 20 or time.hour < 9 
#=> false 
相关问题