Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two nullable dates

I have two date properties and they are exactly the same as i am mapping from one object to another. If I compare them as below it returns false:

if(obj1.d1 == obj2.d1)
{
}

both of them are {01/06/2011 15:44:32}.

I cannot use DateTime.Compare as they are nullable. what would be the best way to compare nullable dates?

like image 948
Zaki Avatar asked Jun 11 '26 14:06

Zaki


2 Answers

Check the actual ticks of those dates, they might be different. Note that DateTime records 100 ns intervals as its smallest unit. Those aren't shown when you format the DateTime.

like image 176
Joey Avatar answered Jun 13 '26 02:06

Joey


You can compare nullable objects, including nullable dates, with the Nullable.Compare<T>() method.

if(Nullable.Compare(obj1.d1, obj2.d1) == 0)
{
}
like image 23
Brad Rem Avatar answered Jun 13 '26 04:06

Brad Rem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!