How can I compare date without Time part in Nullable DateTime? In my model I have datetime field as
public DateTime? AdjustmentDate { get; set; }
I tried to compare dates as
if (!request.reconciliationDate.Equals(DateTime.MinValue))
filterResult = filterResult.Where(rd =>
rd.AdjustmentDate <= request.reconciliationDate.Date).ToList();
and reconciliationDate declared as
public DateTime reconciliationDate { get; set; }
but when I search using reconciliationDate I get the date as {5/20/2020 12:00:00 AM} I enter date as 5/20/2020. I am trying to compare with date 5/20/2020 but doesn’t find it,
How can I take the time part out from nullable DateTime?
I tried DbFunctions.TruncateTime(request.reconciliationDate.Date) but i get DbFunctions doesnt contain definition TruncateTime
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
A Nullable<T> object stores the value of T in the .Value property, and has a handy .HasValue property to let you know if it’s not null. So if you’re comparing a DateTime? with a DateTime, you can do something like:
if (!request.reconciliationDate.Equals(DateTime.MinValue))
filterResult = filterResult
.Where(rd =>
rd.AdjustmentDate.HasValue &&
rd.AdjustmentDate.Value.Date <= request.reconciliationDate.Date)
.ToList();
Method 2
If(dt1.Date == dt2.Date)
Will this not work?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0