Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a date is between two other dates

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
  ...       
like image 610
NickEckhart Avatar asked Nov 28 '22 02:11

NickEckhart


2 Answers

Try

if Time.zone.today.between?(tenant.tenant_from, tenant.tenant_until)
  # YOUR CODE GOES HERE
else
  # YOUR CODE GOES HERE
end
like image 181
Bachan Smruty Avatar answered Dec 11 '22 13:12

Bachan Smruty


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)
like image 30
Jeff Price Avatar answered Dec 11 '22 14:12

Jeff Price