How to check given time is after 3pm or not in Rails?

大憨熊 提交于 2019-12-23 20:21:03

问题


I want easiest or simplest method which returns me boolean value (true/false) depending on the given time is after 3 pm or not irrespective of date.

for ex:-

   def after_three_pm(time)
     //some code here 
   end

   time= Time.now # Tue Jul 26 11:17:27 +0530 2011  THEN
   after_three_pm(time)  # should return false

   time= Time.now # Tue Jul 26 15:17:27 +0530 2011  THEN
   after_three_pm(time)  #  should return true

回答1:


def after_three_pm(time)
  time.hour >= 15
end

is all you need.




回答2:


you simply need to use the "time" function:

    Time.now.hour
=> 11

Now you can just evaluate this against the hour you want.



来源:https://stackoverflow.com/questions/6828294/how-to-check-given-time-is-after-3pm-or-not-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!