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

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

// The date range
DateTime startDate;
DateTime endDate;


DateTime dateToCheck;

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

161347 次浏览

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.

You can use:

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

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)

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)

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

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

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")
]