2017-06-02 50 views
4

我有一个可为空的Date属性,名为BoughtDate。 我尝试以下操作:与MaxValue不起作用的日期比较

if(item.BoughtDate.Value.Equals(DateTime.MaxValue)) item.BoughtDate= null; 

也试过这样:

if(item.BoughtDate.Equals(DateTime.MaxValue)) item.BoughtDate=null; 

调试时,我BoughtDateDateTime.MaxValue似乎一模一样 - 但它说,这是不一样的(不设置我item.BoughtDate为空)

为什么我的比较不起作用?

+0

你需要将它转换为可空日期时间? 'item.BoughtDate =(DateTime?)null;' – Niklas

+2

'BoughtDate'是否来自数据库?用于记录日期的精度差异可能会导致它们表面上看起来相同,但差别仅为几毫秒(通常为3)。 –

+0

'布尔结果= Nullable.Compare(DateTime.Now,yourDate)> 0;' –

回答

0

编辑:正如有人指出它不回答原来的问题,只是提供了一个替代方案。使用DateTime.Compare

https://msdn.microsoft.com/pl-pl/library/system.datetime.compare(v=vs.110).aspx

public static void Main() 
{ 
    DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0); 
    DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0); 
    int result = DateTime.Compare(date1, date2); 
    string relationship; 

    if (result < 0) 
    relationship = "is earlier than"; 
    else if (result == 0) 
    relationship = "is the same time as";   
    else 
    relationship = "is later than"; 

    Console.WriteLine("{0} {1} {2}", date1, relationship, date2); 
} 

在你的榜样,也许这样的

尝试:

if(DateTime.Compare(item.BoughtDate.Value,DateTime.MaxValue) == 0) item.BoughtDate=null; 
+0

这是比较日期时间值的一种很好的替代方法,但是OP询问为什么'equals'API返回'DateTime?'(可空datetime)类型的错误结果。 – RBT

+0

是的,刚刚意识到这一点。 – feni000

5

的问题,如在评论@PaulSuart点,大概是毫秒精确。 DateTime.MaxValue.TimeOfDay{23:59:59.9999999},但您的BoughtDate.TimeOfDay可能是{23:59:59.9990000},所以它们不相等。

我会做的仅仅是比较日期,我认为这是足以在大多数情况下:

if(item.BoughtDate.Value.Date.Equals(DateTime.MaxValue.Date)) item.BoughtDate= null; 
+0

最好添加一个类似于这个答案的扩展:https://stackoverflow.com/a/1005222/68432 - 你的代码很丑陋! –

+0

@PaulSuart在比较2'DateTime'的'Date'属性时发现了什么“丑陋”? – Pikoh

+0

对不起,我很粗鲁。我的意思是说你的代码中的意图可以通过使用扩展方法更清楚地表达出来。 –

0

我做了你的情况下快速测试:

static void Main(string[] args) 
{ 
    DateTime? BoughtDate = DateTime.MaxValue; 
    //subtract 100 milliseconds from it 
    BoughtDate = BoughtDate.Value.AddMilliseconds(-1); 

    Console.WriteLine(BoughtDate.Value.Hour); //23 
    Console.WriteLine(DateTime.MaxValue.Hour); //23 

    Console.WriteLine(BoughtDate.Value.Minute); //59 
    Console.WriteLine(DateTime.MaxValue.Minute); //59 

    Console.WriteLine(BoughtDate.Value.Second); //59 
    Console.WriteLine(DateTime.MaxValue.Second); //59 

    Console.WriteLine(BoughtDate.Value.Year); //9999 
    Console.WriteLine(DateTime.MaxValue.Year); //9999 

    Console.WriteLine(BoughtDate.Value.Month); //12 
    Console.WriteLine(DateTime.MaxValue.Month); //12 

    Console.WriteLine(BoughtDate.Value.Day); //31 
    Console.WriteLine(DateTime.MaxValue.Day); //31 

    Console.WriteLine(BoughtDate.Value.Millisecond); //998 
    Console.WriteLine(DateTime.MaxValue.Millisecond); //999 



    if (BoughtDate.Value.Equals(DateTime.MaxValue)) 
    { 
     Console.WriteLine("equals comparison succeeded"); //doesn't get printed 
    } 
} 

减少只是一个毫秒后从原始值它不会通过if块中的相等检查条件,但如果您在快速观察窗口中看到BoughtDate的值,它们看起来与您所说的完全相同(因为它只显示年,月,日,小时,分钟和第二部分)。

enter image description here

只显示毫秒部分,当您展开+迹象。

所以你的日期时间变量必须以毫秒为单位的差异,即使它们看起来一目了然。