如何知道 C# 中的 DateTime 是否在 DateRange 之间

发布于 2024-10-13 12:32:30 字数 214 浏览 3 评论 0原文

我需要知道日期是否在日期范围之间。我有三个日期:

// The date range
DateTime startDate;
DateTime endDate;

DateTime dateToCheck;

最简单的解决方案是进行比较,但是有没有更聪明的方法来做到这一点?

I need to know if a Date is between a DateRange. I have three dates:

// The date range
DateTime startDate;
DateTime endDate;

DateTime dateToCheck;

The easy solution is doing a comparison, but is there a smarter way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甲如呢乙后呢 2024-10-20 12:32:30

不,做一个简单的比较对我来说看起来不错:

return dateToCheck >= startDate && dateToCheck < endDate;

不过需要考虑的是:

  • DateTime 就时区而言是一种有点奇怪的类型。它可以是UTC,它可以是“本地”,它可以是不明确的。确保你是在将苹果与苹果进行比较。
  • 考虑您的起点和终点应该是包容性的还是排他性的。我已经使上面的代码将其视为包含下界和排除上限。

Nope, doing a simple comparison looks good to me:

return dateToCheck >= startDate && dateToCheck < endDate;

Things to think about though:

  • DateTime is a somewhat odd type in terms of time zones. It could be UTC, it could be "local", it could be ambiguous. Make sure you're comparing apples with apples, as it were.
  • Consider whether your start and end points should be inclusive or exclusive. I've made the code above treat it as an inclusive lower bound and an exclusive upper bound.
浅浅淡淡 2024-10-20 12:32:30

通常我会为此类事情创建 Fowler's Range 实现。

public interface IRange<T>
{
    T Start { get; }
    T End { get; }
    bool Includes(T value);
    bool Includes(IRange<T> range);
}

public class DateRange : IRange<DateTime>         
{
    public DateRange(DateTime start, DateTime end)
    {
        Start = start;
        End = end;
    }

    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }

    public bool Includes(DateTime value)
    {
        return (Start <= value) && (value <= End);
    }

    public bool Includes(IRange<DateTime> range)
    {
        return (Start <= range.Start) && (range.End <= End);
    }
}

用法非常简单:

DateRange range = new DateRange(startDate, endDate);
range.Includes(date)

Usually I create Fowler's Range implementation for such things.

public interface IRange<T>
{
    T Start { get; }
    T End { get; }
    bool Includes(T value);
    bool Includes(IRange<T> range);
}

public class DateRange : IRange<DateTime>         
{
    public DateRange(DateTime start, DateTime end)
    {
        Start = start;
        End = end;
    }

    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }

    public bool Includes(DateTime value)
    {
        return (Start <= value) && (value <= End);
    }

    public bool Includes(IRange<DateTime> range)
    {
        return (Start <= range.Start) && (range.End <= End);
    }
}

Usage is pretty simple:

DateRange range = new DateRange(startDate, endDate);
range.Includes(date)
静水深流 2024-10-20 12:32:30

您可以使用扩展方法使其更具可读性:

public static class DateTimeExtensions
{
    public static bool InRange(this DateTime dateToCheck, DateTime startDate, DateTime endDate)
    {
        return dateToCheck >= startDate && dateToCheck < endDate;
    }
}

现在您可以编写:

dateToCheck.InRange(startDate, endDate)

You could use extension methods to make it a little more readable:

public static class DateTimeExtensions
{
    public static bool InRange(this DateTime dateToCheck, DateTime startDate, DateTime endDate)
    {
        return dateToCheck >= startDate && dateToCheck < endDate;
    }
}

Now you can write:

dateToCheck.InRange(startDate, endDate)
棒棒糖 2024-10-20 12:32:30

您可以使用:

return (dateTocheck >= startDate && dateToCheck <= endDate);

You can use:

return (dateTocheck >= startDate && dateToCheck <= endDate);
七月上 2024-10-20 12:32:30

我发现以下库在进行任何类型的日期数学计算时最有帮助。我仍然对 .Net 框架中没有这样的东西感到惊讶。

http://www.codeproject.com/Articles/168662/Time -NET 时期库

I’ve found the following library to be the most helpful when doing any kind of date math. I’m still amazed nothing like this is part of the .Net framework.

http://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET

So尛奶瓶 2024-10-20 12:32:30

根据谢尔盖的答案,我认为这个更通用的版本更符合福勒的范围想法,并解决了该答案的一些问题,例如能够通过将 T 约束为 IComparable< 来在泛型类中拥有 Includes 方法/代码>。它也是不可变的,就像您所期望的扩展其他值类型(例如 DateTime)功能的类型一样。

public struct Range<T> where T : IComparable<T>
{
    public Range(T start, T end)
    {
        Start = start;
        End = end;
    }

    public T Start { get; }

    public T End { get; }

    public bool Includes(T value) => Start.CompareTo(value) <= 0 && End.CompareTo(value) >= 0;

    public bool Includes(Range<T> range) => Start.CompareTo(range.Start) <= 0 && End.CompareTo(range.End) >= 0;
}

Following on from Sergey's answer, I think this more generic version is more in line with Fowler's Range idea, and resolves some of the issues with that answer such as being able to have the Includes methods within a generic class by constraining T as IComparable<T>. It's also immutable like what you would expect with types that extend the functionality of other value types like DateTime.

public struct Range<T> where T : IComparable<T>
{
    public Range(T start, T end)
    {
        Start = start;
        End = end;
    }

    public T Start { get; }

    public T End { get; }

    public bool Includes(T value) => Start.CompareTo(value) <= 0 && End.CompareTo(value) >= 0;

    public bool Includes(Range<T> range) => Start.CompareTo(range.Start) <= 0 && End.CompareTo(range.End) >= 0;
}
于我来说 2024-10-20 12:32:30

如果有人想要它作为验证器

using System;
using System.ComponentModel.DataAnnotations;

namespace GROOT.Data.Validation;

internal class DateRangeAttribute : ValidationAttribute
{
    public string EndDate;
    public string StartDate;

    public override bool IsValid(object value)
    {
        return (DateTime)value >= DateTime.Parse(StartDate) && (DateTime)value <= DateTime.Parse(EndDate);
    }
}

用法

[DateRange(
    StartDate = "01/01/2020",
    EndDate = "01/01/9999",
    ErrorMessage = "Property is outside of range")
    ]

In case anyone wants it as a Validator

using System;
using System.ComponentModel.DataAnnotations;

namespace GROOT.Data.Validation;

internal class DateRangeAttribute : ValidationAttribute
{
    public string EndDate;
    public string StartDate;

    public override bool IsValid(object value)
    {
        return (DateTime)value >= DateTime.Parse(StartDate) && (DateTime)value <= DateTime.Parse(EndDate);
    }
}

Usage

[DateRange(
    StartDate = "01/01/2020",
    EndDate = "01/01/9999",
    ErrorMessage = "Property is outside of range")
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文