Como comparar datas em c ef compare time

Is there a way to compare two DateTime variables in Linq2Sql but to disregard the Time part.

The app stores items in the DB and adds a published date. I want to keep the exact time but still be able to pull by the date itself.

I want to compare 12/3/89 12:43:34 and 12/3/89 11:22:12 and have it disregard the actual time of day so both of these are considered the same.

I guess I can set all the times of day to 00:00:00 before I compare but I actually do want to know the time of day I just also want to be able to compare by date only.

I found some code that has the same issue and they compare the year, month and day separately. Is there a better way to do this?

1

293

10/22/2019 8:03:31 PM


try using the Date property on the DateTime Object...

if(dtOne.Date == dtTwo.Date) ....

//entityframework.net/knowledge-base/683037/how-to-compare-only-date-without-time-in-datetime-types-in-linq-to-sql-with-entity-framework-#answer-0

518

3/25/2009 7:19:21 PM


For a true comparison, you can use:

dateTime1.Date.CompareTo(dateTime2.Date);


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900

What is New in Camera Decision?




Our Popular TOP Camera Lists

using System;

class GFG {

    public static void Main()

    {

        check(new DateTime(2010, 1, 3, 4, 0, 15),

              new DateTime(2010, 1, 4, 4, 0, 15));

        check(new DateTime(2010, 1, 5, 4, 0, 15),

              new DateTime(2010, 1, 4, 4, 0, 15));

        check(new DateTime(2010, 1, 5, 4, 0, 15),

              new DateTime(2010, 1, 5, 4, 0, 15));

    }

    public static void check(DateTime date1,

                            DateTime date2)

    {

        int value = DateTime.Compare(date1, date2);

        if (value > 0)

            Console.WriteLine(" {0:d} is later than {1:d}. ",

                                               date1, date2);

        else if (value < 0)

            Console.WriteLine(" {0:d} is earlier than {1:d}. ",

                                                 date1, date2);

        else

            Console.WriteLine(" {0:d} is the same as {1:d}. ",

                                                date1, date2);

    }

}

Just use the Date property:

var today = DateTime.Today; var q = db.Games.Where(t => t.StartDate.Date >= today) .OrderBy(t => t.StartDate);

Note that I've explicitly evaluated DateTime.Today once so that the query is consistent - otherwise each time the query is executed, and even within the execution, Today could change, so you'd get inconsistent results. For example, suppose you had data of:

Entry 1: March 8th, 8am Entry 2: March 10th, 10pm Entry 3: March 8th, 5am Entry 4: March 9th, 8pm

Surely either both entries 1 and 3 should be in the results, or neither of them should... but if you evaluate DateTime.Today and it changes to March 9th after it's performed the first two checks, you could end up with entries 1, 2, 4.

Of course, using DateTime.Today assumes you're interested in the date in the local time zone. That may not be appropriate, and you should make absolutely sure you know what you mean. You may want to use DateTime.UtcNow.Date instead, for example. Unfortunately, DateTime is a slippery beast...

EDIT: You may also want to get rid of the calls to DateTime static properties altogether - they make the code hard to unit test. In Noda Time we have an interface specifically for this purpose (IClock) which we'd expect to be injected appropriately. There's a "system time" implementation for production and a "stub" implementation for testing, or you can implement it yourself.

You can use the same idea without using Noda Time, of course. To unit test this particular piece of code you may want to pass the date in, but you'll be getting it from somewhere - and injecting a clock means you can test all the code.

public: static int Compare(DateTime t1, DateTime t2); public static int Compare (DateTime t1, DateTime t2); static member Compare : DateTime * DateTime -> int Public Shared Function Compare (t1 As DateTime, t2 As DateTime) As Integer Int32

A signed number indicating the relative values of t1 and t2.

Value Type Condition
Less than zero t1 is earlier than t2.
Zero t1 is the same as t2.
Greater than zero t1 is later than t2.

Examples

The following example demonstrates the Compare method.

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); // The example displays the following output for en-us culture: // 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM let date1 = DateTime(2009, 8, 1, 0, 0, 0) let date2 = DateTime(2009, 8, 1, 12, 0, 0) let result = DateTime.Compare(date1, date2) let relationship = if result < 0 then "is earlier than" elif result = 0 then "is the same time as" else "is later than" printfn $"{date1} {relationship} {date2}" // The example displays the following output for en-us culture: // 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM Module Example Public Sub Main() Dim date1 As Date = #08/01/2009 12:00AM# Dim date2 As Date = #08/01/2009 12:00PM# Dim result As Integer = DateTime.Compare(date1, date2) Dim relationship As String If result < 0 Then relationship = "is earlier than" ElseIf result = 0 Then relationship = "is the same time as" Else relationship = "is later than" End If Console.WriteLine("{0} {1} {2}", date1, relationship, date2) End Sub End Module ' The example displays the following output: ' 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

Remarks

To determine the relationship of t1 to t2, the Compare method compares the Ticks property of t1 and t2 but ignores their Kind property. Before comparing DateTime objects, ensure that the objects represent times in the same time zone.

Applies to

  • CompareTo(Object)
  • Equals(Object)

Última postagem

Tag