Issue
I have a database table with columns of type dateTime
.
Now I need to see if there already is a row with today’s date, but I don’t know how to compare the column with the current date without the hour, minutes, seconds.
Basically I have 2022-02-04 14:06:21.080
and I need to check if there is a row created on 2022-02-04
.
I’m looking for something like
if (db.dates.Where(x => x.SentDate == Date.Now).Count() > 0)
{
// Do something
}
else
{
// Do something else
}
I only need to see if it has a date from today it doesn’t matter what time it was created.
Any help is much appreciated!
Solution
You can check a date range
var today = DateTime.Today;
var tomorrow = today.AddDays(1);
if(db.dates.Where(x => x.SentDate >= today && x.SentDate < tomorrow) ...
The DateTime.Today Property gets the current date with the time component set to 00:00:00.
Note that we test the lower bound with >= today
(with today
meaning today at 00:00:00) but the upper one with < tomorrow
, since we do not want to include tomorrow at 00:00:00.
Answered By – Olivier Jacot-Descombes
Answer Checked By – Clifford M. (BugsFixing Volunteer)