I have a Model called "Tenant" and it has two Date attributes tenant_from
and tenant_until
.
I want to write a method that checks if Date.today
is between the two dates I mentioned above. I want to do something along these lines:
IF Date.today IS BETWEEN tenant.tenant_from AND tenant.tenant_until DO
...
ELSE
...
Try
if Time.zone.today.between?(tenant.tenant_from, tenant.tenant_until)
# YOUR CODE GOES HERE
else
# YOUR CODE GOES HERE
end
Just for an alternate solution. You can use a Date range and cover?
to achieve the same effect.
(tenant.tenant_from .. tenant.tenant_until).cover?(Date.today)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With