93

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?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219

7 Answers7

157

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.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yes. Making sure that the `DateTime`s you compare are of the same kind(UTC/Local) is important. With different kinds the raw time will be compared instead of converting both to a common kind. – CodesInChaos Jan 24 '11 at 11:57
  • 11
    `return startDate <= dateToCheck && dateToCheck < endDate` seems slightly more readable. – Mauricio Morales Jun 10 '14 at 19:06
  • 10
    @MauricioMorales: It depends; some people find it easier to read "the bit that's varying" (the date to check) being on the left hand side consistently. That's what I tend to do. I can see the advantages of the "in chronological order" approach too, but I think I'd personally prefer the way I've got it. – Jon Skeet Jun 10 '14 at 19:07
  • @MauricioMorales when deciding which side of a comparison to write first, I always pick the object being checked, instead of the value it is being compared to. That seems most logical to me. So in this case, where `dateToCheck` is the object being checked, I would write it first (on both sides of the `&&`) like @JonSkeet did. – Zero3 Sep 06 '22 at 13:21
72

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)
Remco te Wierik
  • 1,436
  • 2
  • 10
  • 10
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 3
    Just a silly observation, doesn't the constructor need to ensure that start is less than end, if it doesn't, it would break the logic ... especially on the Includes(IRange range) – Adrian Hum Jan 08 '16 at 03:35
  • 2
    Man this is such an good solution it even work with LINQ. Like so: var valueFromVacationsList =vacationBookingsForThisMonth.FirstOrDefault(s => (s.Id == currentUser.Id)&&new DateRange(s.StartDateTime, s.EndDateTime).Includes(itemDate)); – Kadaj Oct 20 '16 at 14:51
56

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)
Elian Ebbing
  • 18,779
  • 5
  • 48
  • 56
9

You can use:

return (dateTocheck >= startDate && dateToCheck <= endDate);
WraithNath
  • 17,658
  • 10
  • 55
  • 82
5

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

landerud
  • 51
  • 1
  • 1
  • I agree. I cannot believe .NET has no native date math assemblies. Thanks for sharing the library – BoundForGlory Jun 01 '16 at 17:54
  • I used the Time Period library for a project that created schedules for when electronic signs were supposed to display images and it was a real time-saver. Heck, I can't imagine having completed that project w/o the Time Period library. – PoorInRichfield Nov 12 '18 at 14:53
3

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;
}
Neo
  • 4,145
  • 6
  • 53
  • 76
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")
    ]
David
  • 1,131
  • 9
  • 22