Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time comparison in ruby

I want to check whether the current time is in between 10AM and 10PM. How can I do the check?

like image 695
Sayuj Avatar asked Jul 16 '26 12:07

Sayuj


1 Answers

You can define a Range and check for the inclusion.

(10..22).include?(Time.new.hour)
# => true

Your code will be

if (10..22).include?(Time.new.hour)
  # do whatever you want
end
like image 115
Simone Carletti Avatar answered Jul 19 '26 02:07

Simone Carletti