Date comparison with MaxValue does not work

I have a nullable Date property called BoughtDate. I am trying to do 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 mine BoughtDateand it DateTime.MaxValueseems exactly the same, but it says that it is not the same (does not set mine item.BoughtDateto null)

Why doesn't my comparison work?

+4
source share
4 answers

The problem, since the @PaulSuart points in the comments is probably millisecond precision. DateTime.MaxValue.TimeOfDay {23:59:59.9999999}but yours BoughtDate.TimeOfDayis probably {23:59:59.9990000}why they are not equal.

What would I do by simply comparing the dates, I think this is sufficient in most cases:

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

EDIT: -, , .

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

:

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

+.

, , .

0

I think you can find the answer in the link below if the Date you are retrieving from SQL-Server. The SQL server only supports the time range up to 23: 59: 59.997, and this can lead to fewer ticks than C # supports. You can simply compare the dates, as @Pikoh already mentioned.

https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql

0
source

Source: https://habr.com/ru/post/1678393/


All Articles