c#中是否有像SQL一样的BETWEEN DateTime ?

本文关键字:一样 BETWEEN DateTime 是否 SQL | 更新日期: 2023-09-27 17:50:35

在c#中有DateTime吗?我知道我可以用if (a > date1 && a < date2)做简单的检查,但我试图找到Between方法。

c#中是否有像SQL一样的BETWEEN DateTime ?

没有Between功能,但应该很容易添加一个

public static bool Between(DateTime input, DateTime date1, DateTime date2)
{
    return (input > date1 && input < date2);
}

为什么只限制日期,使用IComparable接口

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

没有。

BETWEEN是包含WRT的边界,而不是排他的边界。不管怎样,给你:

public static bool Between(this DateTime instant, DateTime dtFrom , DateTime dtThru )
{
  if (dtFrom > dtThru) throw new ArgumentException( "dtFrom may not be after dtThru", "dtFrom" );
  bool isBetween = ( instant >= dtFrom && instant <= dtThru );
  return isBetween;
}

基于@richardschneider的答案,我的解决方案添加了一个边界范围类型作为参数。

public enum RangeBoundaryType
{
    [Description("Exclusive")]
    Exclusive,
    [Description("Inclusive on both boundaries")]
    Inclusive,
    [Description("Inclusive on only the lower boundary")]
    InclusiveLowerBoundaryOnly,
    [Description("Inclusive on only the upper boundary")]
    InclusiveUpperBoundaryOnly
}
public static bool Between(this IComparable value, IComparable comparator0, IComparable comparator1, RangeBoundaryType rangeBoundary)
{
    switch (rangeBoundary)
    {
        case RangeBoundaryType.Exclusive:
            return (value.CompareTo(comparator0) > 0 && value.CompareTo(comparator1) < 0);
        case RangeBoundaryType.Inclusive:
            return (value.CompareTo(comparator0) >= 0 && value.CompareTo(comparator1) <= 0);
        case RangeBoundaryType.InclusiveLowerBoundaryOnly:
            return (value.CompareTo(comparator0) >= 0 && value.CompareTo(comparator1) < 0);
        case RangeBoundaryType.InclusiveUpperBoundaryOnly:
            return (value.CompareTo(comparator0) > 0 && value.CompareTo(comparator1) <= 0);
        default:
            return false;
    }
}

您可以添加一个扩展方法:

public static Boolean Between(this DateTime input, DateTime minDate, DateTime maxDate)
{
    // SQL takes limit in !
    return input >= minDate && input <= maxDate;
}

我使用类似于Richard Schneider的(通用之间)和Gary Pendlebury的答案(更简单的可配置边界包含)

public static bool Between(this IComparable value, IComparable lowerBoundary, IComparable upperBoundary, 
    bool includeLowerBoundary=true, bool includeUpperBoundary=true)
{
    var lower = value.CompareTo(lowerBoundary);
    var upper = value.CompareTo(upperBoundary);
    return (lower > 0 || (includeLowerBoundary && lower == 0)) &&
           (upper < 0 || (includeUpperBoundary && upper == 0));
}

这种方法快速且参数可逆:

public static bool BetweenInclusive(this DateTime value, DateTime a, DateTime b)
{
    return ((a <= value) && (value <= b)) || ((b <= value) && (value <= a));
}

没有,但是如果您遵守每个代码完成的数字行格式,原始代码看起来更简单:

if((lowDate < a) && (a < highDate)) 

我的版本(基于@richardschneider的答案)与参数化包容性:

public static bool IsBetween(this IComparable value, IComparable lower, IComparable upper, bool isInclusive = true)
    {
        if (isInclusive)
        {
            return value.CompareTo(lower) >= 0 && value.CompareTo(upper) <= 0;
        }
        else
        {
            return value.CompareTo(lower) > 0 && value.CompareTo(upper) < 0;
        }
    }