2016-04-15 50 views

回答

2

這是挑出小時,檢查它是否在一定範圍內的問題:

(9..19).include?(Time.current.hour) 

如果你想有一天的不同時間多封郵件:

case (Time.current.hour) 
when 0..9, 21..23 
    "Late" 
when 9..19 
    "Prime" 
else 
    "Regular" 
end 
+0

這是否考慮時區? –

+0

它使用'Time.current'發出的任何時區,這可能取決於當前請求的'Time.zone'設置。如果你想讓它不可知:'Time.current.utc'。 – tadman

7

你可以始終使用between?

這是一個漂亮的小方法

Time.current.between?('09:00 UTC-05:00', '19:00 UTC-05:00') 

所以它會很好地迴應你,如果

0

你可以做到這一點,它返回真/假:

time = Time.new 
if time.hour >= 9 && time.hour < 19 
    // Your code 
end 
+0

Rails默認使用您的操作系統的時區。 –

相關問題