4

How to compare only Date without Time in DateTime types in C#.One of the date will be nullable.How can i do that??

3
  • 1
    I don't understand your question. Can you please clarify it? Just use DateTime? < DateTime ? Commented Oct 16, 2014 at 12:21
  • i want to compare two datetime object without conisdering time..one of date object is nullable type Commented Oct 16, 2014 at 12:22
  • Do you need to handle different timezones, daylight savings time transitions, etc.? Commented Oct 16, 2014 at 12:30

7 Answers 7

6
DateTime val1;
DateTime? val2;

if (!val2.HasValue)
    return false;

return val1.Date == val2.Value.Date;
Sign up to request clarification or add additional context in comments.

2 Comments

...+1. Or trenary operator: return val2.HasValue ? val1.Date == val2.Value.Date : false;
@DmitryBychenko - The conditional operator seems a bit cumbersome here. How about just return val2.HasValue && val1.Date == val2.Value.Date; ?
2

You can use the Date property of DateTime object

Datetime x;
Datetime? y;

if (y != null && y.HasValue && x.Date == y.Value.Date)
{
 //DoSomething
}

1 Comment

Fails if y is null.
0

This uses short-circuit to avoid comparisons if nullable date is null, then uses the DateTime.Date property to determine equivalency.

bool Comparison(DateTime? nullableDate, DateTime aDate) {
    if(nullableDate != null && aDate.Date == nullableDate.Value.Date) {
        return true;
    }

    return false;
}

Comments

0
bool DatesMatch(DateTime referenceDate, DateTime? nullableDate)
{
    return (nullableDate.HasValue) ? 
        referenceDate.Date == nullableDate.Value.Date : 
        false;
}

Comments

0

If you want a true comparison, you can use:

    Datetime dateTime1
    Datetime? dateTime2

    if(dateTime2.Date != null)
       dateTime1.Date.CompareTo(dateTime2.Date);

Hope it helps...

Comments

0

The only challenging aspect here is the fact that you want something which is both DateTime and nullable.

Here is the solution for standard DateTime: How to compare only Date without Time in DateTime types in C#?

if(dtOne.Date == dtTwo.Date)

For nullable, it's just a matter of choice. I would choose an extension method.

class Program
{
    static void Main(string[] args)
    {
        var d1 = new DateTime(2000, 01, 01, 12, 24, 48);
        DateTime? d2 = new DateTime(2000, 01, 01, 07, 29, 31);

        Console.WriteLine((d1.Date == ((DateTime)d2).Date));

        Console.WriteLine((d1.CompareDate(d2)));
        Console.WriteLine((d1.CompareDate(null)));

        Console.WriteLine("Press enter to continue...");
        Console.ReadLine();
    }
}

static class DateCompare
{
    public static bool CompareDate(this DateTime dtOne, DateTime? dtTwo)
    {
        if (dtTwo == null) return false;
        return (dtOne.Date == ((DateTime)dtTwo).Date);
    }
}

1 Comment

Instead of casting dtTwo you can get the date through dtTwo.Value.Date
0

You could create a method like the one below, following the similar return values as the .net framework comparisons, giving -1 when the left side is smallest, 0 for equal dates, and +1 for right side smallest:

    private static int Compare(DateTime? firstDate, DateTime? secondDate)
    {
        if(!firstDate.HasValue && !secondDate.HasValue)
            return 0;
        if (!firstDate.HasValue)
            return -1;
        if (!secondDate.HasValue)
            return 1;
        else 
            return DateTime.Compare(firstDate.Value.Date, secondDate.Value.Date);
    }

Of Course a nicer implementation would be to create an extension method for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.