简体   繁体   English

可空-如何在C#中的DateTime类型中仅比较没有日期的Date?

[英]Nullable-How to compare only Date without Time in DateTime types in C#?

如何在C#中的DateTime类型中仅比较没有时间的日期,其中一个日期可以为空,我该怎么做?

DateTime val1;
DateTime? val2;

if (!val2.HasValue)
    return false;

return val1.Date == val2.Value.Date;

You can use the Date property of DateTime object 您可以使用DateTime对象的Date属性

Datetime x;
Datetime? y;

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

This uses short-circuit to avoid comparisons if nullable date is null, then uses the DateTime.Date property to determine equivalency. 如果可以为null的日期为null,则使用短路来避免比较,然后使用DateTime.Date属性确定等效项。

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

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

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... 希望能帮助到你...

The only challenging aspect here is the fact that you want something which is both DateTime and nullable. 这里唯一具有挑战性的方面是您想要的东西既是DateTime又是可空的。

Here is the solution for standard DateTime: How to compare only Date without Time in DateTime types in C#? 这是标准DateTime的解决方案: 如何在C#中的DateTime类型中仅比较没有日期的Date?

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);
    }
}

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: 您可以创建一种类似于以下方法的方法,其返回值与.net框架比较类似,当左侧最小时为-1,相等的日期为0,右侧最小为+1。

    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. 当然,更好的实现是为此创建扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM