I have an event with start_time
and end_time
and want to check if the event is "in progress". That would be to check if today's date is in the range between the two dates.
How would you do this in a function?
I have an event with start_time
and end_time
and want to check if the event is "in progress". That would be to check if today's date is in the range between the two dates.
How would you do this in a function?
In Ruby 1.9.2
===
doesn't work, I get an error:Instead use
#cover?
:If you're using Rails you can use
TimeWithZone#between?
. You'd then have something like this:summary
Use
===
Actually, there is an operator that will do this. Make a
Range
and compareTime
objects to it using the===
operator.Update post-comment-flood: This version works well everywhere. (In Rails, in Ruby 1, and in Ruby 2.) The earlier
irb
example also worked fine but the interactive example wasn't always reproduced correctly in some experiments. This one is easier to cut-and-paste.It's all straightened out now.
Checked is current date in between two dates. Using Ruby
Because the date class includes the Comparable module, every date object has a
between?
method.