[RESOLVED]How to check if date is between two dates in Controller ?

Hi

i have an table with name Doctor_Out with fields "ID – Doctor_ID – Frm_Date – To_Date"

now i try to check if the date i typed in textbox is between "Frm_Date and To_Date" or not so i try :

1.  add data in Doctor_Out table as:

Id = 1

Doctor_ID = 1

Frm_Date = 2014-10-01

To_Date = 2014-10-15

now i have enter date "2014-10-09" in my textbox and try code to check if "visitDatas.Visit_Date" is between Frm_Date and To_Date  or not but i got result as null ?

var Result = db.Doctor_Out.Where(date => date.Frm_Date >= visitDatas.Visit_Date && date.To_Date <= visitDatas.Visit_Date);

and  if make try Condition "==" i got data :

var Result = db.Doctor_Out.Where(date => date.Frm_Date == visitDatas.Visit_Date);

so Please how can i check if the date i typed is between "Frm_Date" or "To_Date" or not ?

If you need to check if your Date is between two other dates, you’ll just use a few logical operators. It can be confusing at times, but it looks like you had your operators switched up a bit. Sometimes it can help to write it out in a more readable (from
left to right
) fashion as seen below :

var Result = db.Doctor_Out.Where(d=> visitDatas.Visit_Date >= d.Frm_Date && visitDatas.Visit_Date <= d.To_Date);

This states that your date will be greater than or equal to the From Date and less than or equal to the To Date.

Thanks a lot Rion Smile

Leave a Reply