【问题标题】:Nullable-How to compare only Date without Time in DateTime types in C#?Nullable-如何在 C# 中的 DateTime 类型中仅比较没有时间的日期?
【发布时间】:2014-12-11 19:32:59
【问题描述】:

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

【问题讨论】:

  • 我不明白你的问题。你能澄清一下吗?只需使用DateTime? < DateTime ?
  • 我想在不考虑时间的情况下比较两个日期时间对象。日期对象之一是可为空的类型
  • 您需要处理不同的时区、夏令时转换等吗?

标签: c# .net datetime


【解决方案1】:
DateTime val1;
DateTime? val2;

if (!val2.HasValue)
    return false;

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

【讨论】:

  • ...+1。或三元运算符:return val2.HasValue ? val1.Date == val2.Value.Date : false;
  • @DmitryBychenko - 条件运算符在这里看起来有点麻烦。 return val2.HasValue && val1.Date == val2.Value.Date; 怎么样?
【解决方案2】:

您可以使用DateTime 对象的Date 属性

Datetime x;
Datetime? y;

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

【讨论】:

  • 如果y 为空则失败。
【解决方案3】:

如果可空日期为空,则使用短路来避免比较,然后使用 DateTime.Date 属性来确定等效性。

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

    return false;
}

【讨论】:

    【解决方案4】:
    bool DatesMatch(DateTime referenceDate, DateTime? nullableDate)
    {
        return (nullableDate.HasValue) ? 
            referenceDate.Date == nullableDate.Value.Date : 
            false;
    }
    

    【讨论】:

      【解决方案5】:

      如果你想要一个真实的比较,你可以使用:

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

      希望对你有帮助...

      【讨论】:

        【解决方案6】:

        这里唯一具有挑战性的方面是您想要既可以 DateTime 又可以为空的东西。

        这是标准日期时间的解决方案:How to compare only Date without Time in DateTime types in C#?

        if(dtOne.Date == dtTwo.Date)
        

        对于可为空的,这只是一个选择问题。我会选择一种扩展方法。

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

        【讨论】:

        • 您可以通过 dtTwo.Value.Date 获取日期,而不是强制转换 dtTwo
        【解决方案7】:

        您可以创建如下所示的方法,遵循与 .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);
            }
        

        当然,更好的实现是为此创建一个扩展方法。

        【讨论】:

          猜你喜欢
          • 2010-10-15
          • 1970-01-01
          • 1970-01-01
          • 2010-12-01
          • 1970-01-01
          • 2016-07-02
          • 2012-01-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多