2017-06-02 51 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

只顯示毫秒部分,當您展開+跡象。

所以你的日期時間變量必須以毫秒爲單位的差異,即使它們看起來一目瞭然。