Is there BETWEEN DateTime in C# just like SQL does?

前端 未结 9 1194
悲&欢浪女
悲&欢浪女 2021-01-01 09:13

Is there between DateTime in C# ? I know I can do simple check with if (a > date1 && a < date2) but I was trying to find Between meth

9条回答
  •  再見小時候
    2021-01-01 09:35

    Why restrict to just dates, use the IComparable interface.

    public static bool InclusiveBetween (this IComparable a, IComparable b, IComparable c)
    {
        return a.CompareTo(b) >= 0 && a.CompareTo(c) <= 0;
    }
    
    public static bool ExclusiveBetween (this IComparable a, IComparable b, IComparable c)
    {
        return a.CompareTo(b) > 0 && a.CompareTo(c) < 0;
    }
    
    public static bool SqlBetween (this IComparable a, IComparable b, IComparable c)
    {
        return a.InclusiveBetween(b, c);
    }
    

提交回复
热议问题