Skip to content

DateRange

Struct sealedpublic
Represents an inclusive date range defined by a start and end boundary, with helpers for containment checks, overlap detection, and duration calculation.
Namespace SampleApi
Assembly SampleApi
Package v1.0.0.0
public sealed struct DateRange
: System.IEquatable<SampleApi.DateRange>
IEquatable<DateRange>
Implements
IEquatable<DateRange>
classDiagram
    direction TB
    class DateRange {
        <<struct>>
    }
    class IEquatable~DateRange~ {
        <<interface>>
    }
    IEquatable~DateRange~ <|.. DateRange
DateRange is a readonly struct that implements IEquatable`1 for efficient, allocation-free value comparisons. The constructor enforces the invariant that Start must be on or before End, throwing ArgumentException otherwise. Common use cases include filtering orders by date, defining subscription periods, and computing business-day durations. The DateTimeOffset) and DateRange) methods enable calendar-style range queries.
DateRange.DateRange(
DateTimeOffset start,
DateTimeOffset end)

Initializes a new instance of the DateRange struct.

start DateTimeOffset
The inclusive start date of the range.
end DateTimeOffset
The inclusive end date of the range.
ArgumentException
Thrown when start is after end.
TimeSpan
TimeSpan DateRange.Duration

Gets the duration of time between Start and End.

DateTimeOffset
DateTimeOffset DateRange.End

Gets the inclusive end date of the range.

DateTimeOffset
DateTimeOffset DateRange.Start

Gets the inclusive start date of the range.

bool DateRange.Contains(
DateTimeOffset date)

Determines whether the specified date falls within this range.

date DateTimeOffset
The date to check.
bool true if date is within the range; otherwise, false.
bool DateRange.Equals(
DateRange other)
other DateRange
Equals Section titled Equals override
bool
bool DateRange.Equals(
object? obj)
obj object?
GetHashCode Section titled GetHashCode override
int
int DateRange.GetHashCode()
op_Equality Section titled op_Equality static
bool
bool DateRange.operator ==(
DateRange left,
DateRange right)

Determines whether two DateRange instances are equal.

left DateRange
right DateRange
op_Inequality Section titled op_Inequality static
bool
bool DateRange.operator !=(
DateRange left,
DateRange right)

Determines whether two DateRange instances are not equal.

left DateRange
right DateRange
bool DateRange.Overlaps(
DateRange other)

Determines whether this range overlaps with another range.

other DateRange
The other date range to test.
bool true if the ranges overlap; otherwise, false.
ToString Section titled ToString override
string
string DateRange.ToString()

Returns a human-readable representation of the date range.

Creating a date range and testing containment:

var q1 = new DateRange(
new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero),
new DateTimeOffset(2026, 3, 31, 23, 59, 59, TimeSpan.Zero)
);
bool inRange = q1.Contains(DateTimeOffset.UtcNow);
Console.WriteLine($"Q1 duration: {q1.Duration.TotalDays} days");

Checking whether two ranges overlap:

var feb = new DateRange(
new DateTimeOffset(2026, 2, 1, 0, 0, 0, TimeSpan.Zero),
new DateTimeOffset(2026, 2, 28, 23, 59, 59, TimeSpan.Zero)
);
bool overlaps = q1.Overlaps(feb); // true