I have a nullable Date property called BoughtDate
.
I am trying the following:
if( item.BoughtDate.Value.Equals( DateTime.MaxValue) ) item.BoughtDate= null;
also tried this:
if( item.BoughtDate.Equals( DateTime.MaxValue) ) item.BoughtDate=null;
When debugging, my BoughtDate
and DateTime.MaxValue
seems exactly the same - yet it says it is not the same(does not set my item.BoughtDate
to null)
Why does my comparison not work?
The problem,as @PaulSuart points in the comments, is probably the milliseconds precision. DateTime.MaxValue.TimeOfDay
is {23:59:59.9999999}
,but your BoughtDate.TimeOfDay
is probably {23:59:59.9990000}
,so they are not equal.
What i would do is just comparing the dates, i think that's enough in most cases:
if( item.BoughtDate.Value.Date.Equals( DateTime.MaxValue.Date) ) item.BoughtDate= null;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With