aksil
aksil

Reputation: 31

Comparing two dates in asp.net mvc

I want to compare two dates but i don't know what is the problem ?? someone help me

enter image description here

  [HttpPost]
    public JsonResult Checkdate(DateTime startdate)
    {
            Entities1 db = new Entities1();
    bool isValid = !db.Events.ToList().Exists(p => p.StartDate.Equals(startdate, StringComparison.CurrentCultureIgnoreCase));
        return Json(isValid);
    }

Upvotes: 0

Views: 666

Answers (3)

Adina
Adina

Reputation: 123

The other answers should work but just to include this try looking for:

bool exists = false;
var event = db.Events.Where(p => p.StartDate == startdate);
if (event != null){
    exists = true;
}

this way not only do you know if it's true but you have a place to hold it and see it later

you can actually return it in the Json if you'd like

Upvotes: 0

danghyan
danghyan

Reputation: 111

Why wouldn't you just use the following?

db.Events.Any(p => p.StartDate == startdate);

It can be used with or without .ToList().

Upvotes: 1

Arthur Cam
Arthur Cam

Reputation: 599

I do not understand what you are trying to achieve, but for date time comparisons, it is wiser to use Datetime.Compare(date1, date2) method.

like:

bool isValid = DateTime.Compare(p.StartDate, startDate) == 0;

Upvotes: 0

Related Questions