Daniel Peñalba
Daniel Peñalba

Reputation: 31897

How to know if a DateTime is between a DateRange in C#

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?

Upvotes: 98

Views: 189908

Answers (7)

David
David

Reputation: 1196

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

Upvotes: 0

Neo
Neo

Reputation: 4497

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

Upvotes: 5

landerud
landerud

Reputation: 51

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

Upvotes: 5

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236308

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)

Upvotes: 76

Elian Ebbing
Elian Ebbing

Reputation: 19067

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)

Upvotes: 63

WraithNath
WraithNath

Reputation: 18013

You can use:

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

Upvotes: 10

Jon Skeet
Jon Skeet

Reputation: 1503869

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.

Upvotes: 166

Related Questions