In Linq to SQL with Entity Framework, how do you compare solely Date without Time in DateTime types?

.net c# database entity-framework linq-to-sql

Question

Is there a way to compare two DateTime variables in Linq2Sql but to disregard the Time part.

The app stores items in the DB and adds a published date. I want to keep the exact time but still be able to pull by the date itself.

I want to compare 12/3/89 12:43:34 and 12/3/89 11:22:12 and have it disregard the actual time of day so both of these are considered the same.

I guess I can set all the times of day to 00:00:00 before I compare but I actually do want to know the time of day I just also want to be able to compare by date only.

I found some code that has the same issue and they compare the year, month and day separately. Is there a better way to do this?

1
293
10/22/2019 8:03:31 PM

Accepted Answer

try using the Date property on the DateTime Object...

if(dtOne.Date == dtTwo.Date)
    ....
518
3/25/2009 7:19:21 PM

Popular Answer

For a true comparison, you can use:

dateTime1.Date.CompareTo(dateTime2.Date);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow