Noda Time
Show / Hide Table of Contents

Class DateInterval

An interval between two dates.
Since 2.0.x
Availability netstandard2.0
Inheritance
Object
DateInterval
Implements
IEquatable<DateInterval>
IEnumerable<LocalDate>
IEnumerable
Inherited Members
Object.Equals(Object, Object)
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
public sealed class DateInterval : IEquatable<DateInterval>, IEnumerable<LocalDate>, IEnumerable
Remarks

Equality is defined in a component-wise fashion: two date intervals are considered equal if their start dates are equal to each other and their end dates are equal to each other. Ordering between date intervals is not defined.

The two dates must be in the same calendar, and the end date must not be earlier than the start date.

The end date is deemed to be part of the range, as this matches many real life uses of date ranges. For example, if someone says "I'm going to be on holiday from Monday to Friday," they usually mean that Friday is part of their holiday.

Constructors

DateInterval(LocalDate, LocalDate)

Constructs a date interval from a start date and an end date, both of which are included in the interval.
Since 2.0.x
Availability netstandard2.0
Declaration
public DateInterval(LocalDate start, LocalDate end)
Parameters
Type Name Description
LocalDate start Start date of the interval
LocalDate end End date of the interval
Sample snippet
using NodaTime;
using System;

var calendar = CalendarSystem.Gregorian;
LocalDate start = new LocalDate(2017, 1, 1, calendar);
LocalDate end = new LocalDate(2017, 12, 31, calendar);

DateInterval interval = new DateInterval(start, end);

Console.WriteLine(interval.Length);
Console.WriteLine(interval.ToString());
Console.WriteLine(interval.Start);
Console.WriteLine(interval.End);
Console.WriteLine(interval.Calendar);

Output:

365
[2017-01-01, 2017-12-31]
Sunday, 01 January 2017
Sunday, 31 December 2017
Gregorian

Exceptions
Type Condition
ArgumentException end is earlier than start or the two dates are in different calendars.

Properties

Calendar

Gets the calendar system of the dates in this interval.
Since 2.3.x
Availability netstandard2.0
Declaration
public CalendarSystem Calendar { get; }
Property Value
Type Description
CalendarSystem The calendar system of the dates in this interval.

End

Gets the end date of the interval.
Since 2.0.x
Availability netstandard2.0
Declaration
public LocalDate End { get; }
Property Value
Type Description
LocalDate The end date of the interval.

Length

Gets the length of this date interval in days. This will always be at least 1.
Since 2.0.x
Availability netstandard2.0
Declaration
public int Length { get; }
Property Value
Type Description
Int32 The length of this date interval in days.

Start

Gets the start date of the interval.
Since 2.0.x
Availability netstandard2.0
Declaration
public LocalDate Start { get; }
Property Value
Type Description
LocalDate The start date of the interval.

Methods

Contains(DateInterval)

Checks whether the given interval is within this interval. This requires that the start date of the specified interval is not earlier than the start date of this interval, and the end date of the specified interval is not later than the end date of this interval.
Since 2.3.x
Availability netstandard2.0
Declaration
public bool Contains(DateInterval interval)
Parameters
Type Name Description
DateInterval interval The interval to check for containment within this interval.
Returns
Type Description
Boolean true if interval is within this interval; false otherwise.
Remarks
An interval contains another interval with same start and end dates, or itself.
Sample snippet
using NodaTime;
using System;

LocalDate start = new LocalDate(2017, 1, 1);
LocalDate end = new LocalDate(2017, 12, 31);

DateInterval interval = new DateInterval(start, end);
DateInterval june = new DateInterval(
    new LocalDate(2017, 6, 1),
    new LocalDate(2017, 6, 30));

var result = interval.Contains(june);
Console.WriteLine(result);

Output:

True

Exceptions
Type Condition
ArgumentException interval uses a different calendar to this date interval.

Contains(LocalDate)

Checks whether the given date is within this date interval. This requires that the date is not earlier than the start date, and not later than the end date.
Since 2.0.x
Availability netstandard2.0
Declaration
public bool Contains(LocalDate date)
Parameters
Type Name Description
LocalDate date The date to check for containment within this interval.
Returns
Type Description
Boolean true if date is within this interval; false otherwise.
Sample snippet
using NodaTime;
using System;

LocalDate start = new LocalDate(2017, 1, 1);
LocalDate end = new LocalDate(2017, 12, 31);

DateInterval interval = new DateInterval(start, end);

var result = interval.Contains(new LocalDate(2017, 12, 5));
Console.WriteLine(result);

Output:

True

Exceptions
Type Condition
ArgumentException date is not in the same calendar as the start and end date of this interval.

Deconstruct(out LocalDate, out LocalDate)

Deconstruct this date interval into its components.
Since 2.3.x
Availability netstandard2.0
Declaration
public void Deconstruct(out LocalDate start, out LocalDate end)
Parameters
Type Name Description
LocalDate start The LocalDate representing the start of the interval.
LocalDate end The LocalDate representing the end of the interval.
Sample snippet
using NodaTime;
using System;

LocalDate start = new LocalDate(2017, 1, 1);
LocalDate end = new LocalDate(2017, 12, 31);

DateInterval value = new DateInterval(start, end);
value.Deconstruct(out LocalDate actualStart, out LocalDate actualEnd);

Console.WriteLine(actualStart);
Console.WriteLine(actualEnd);

Output:

Sunday, 01 January 2017
Sunday, 31 December 2017

Equals(DateInterval)

Compares the given date interval for equality with this one. See the type documentation for a description of equality semantics.
Since 2.0.x
Availability netstandard2.0
Declaration
public bool Equals(DateInterval other)
Parameters
Type Name Description
DateInterval other The date interval to compare this one with.
Returns
Type Description
Boolean True if this date interval has the same same start and end date as the one specified.

Equals(Object)

Compares the given object for equality with this one, as per Equals(DateInterval). See the type documentation for a description of equality semantics.
Since 2.0.x
Availability netstandard2.0
Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
Object obj The value to compare this one with.
Returns
Type Description
Boolean true if the other object is a date interval equal to this one, consistent with Equals(DateInterval).
Overrides
System.Object.Equals(System.Object)

GetEnumerator()

Returns an enumerator for the dates in the interval, including both Start and End.
Since 2.3.x
Availability netstandard2.0
Declaration
public IEnumerator<LocalDate> GetEnumerator()
Returns
Type Description
IEnumerator<LocalDate> An enumerator for the interval.

GetHashCode()

Returns the hash code for this interval, consistent with Equals(DateInterval). See the type documentation for a description of equality semantics.
Since 2.0.x
Availability netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type Description
Int32 The hash code for this interval.
Overrides
System.Object.GetHashCode()

Intersection(DateInterval)

Returns the intersection between the given interval and this interval.
Since 2.3.x
Availability netstandard2.0
Declaration
public DateInterval Intersection(DateInterval interval)
Parameters
Type Name Description
DateInterval interval The specified interval to intersect with this one.
Returns
Type Description
DateInterval A DateInterval corresponding to the intersection between the given interval and the current instance. If there is no intersection, a null reference is returned.
Sample snippet
using NodaTime;
using System;

DateInterval januaryToAugust = new DateInterval(
    new LocalDate(2017, 1, 1),
    new LocalDate(2017, 8, 31));

DateInterval juneToNovember = new DateInterval(
    new LocalDate(2017, 6, 1),
    new LocalDate(2017, 11, 30));

DateInterval juneToAugust = new DateInterval(
    new LocalDate(2017, 6, 1),
    new LocalDate(2017, 8, 31));

var result = januaryToAugust.Intersection(juneToNovember);
Console.WriteLine(result);

Output:

[2017-06-01, 2017-08-31]

Exceptions
Type Condition
ArgumentException interval uses a different calendar to this date interval.

ToString()

Returns a string representation of this interval.
Since 2.0.x
Availability netstandard2.0
Declaration
public override string ToString()
Returns
Type Description
String A string representation of this interval, as [start, end], where "start" and "end" are the dates formatted using an ISO-8601 compatible pattern.
Overrides
System.Object.ToString()

Union(DateInterval)

Returns the union between the given interval and this interval, as long as they're overlapping or contiguous.
Since 2.3.x
Availability netstandard2.0
Declaration
public DateInterval Union(DateInterval interval)
Parameters
Type Name Description
DateInterval interval The specified interval from which to generate the union interval.
Returns
Type Description
DateInterval A DateInterval corresponding to the union between the given interval and the current instance, in the case the intervals overlap or are contiguous; a null reference otherwise.
Sample snippet
using NodaTime;
using System;

DateInterval firstInterval = new DateInterval(
    new LocalDate(2014, 3, 7),
    new LocalDate(2014, 3, 20));

DateInterval secondInterval = new DateInterval(
    new LocalDate(2014, 3, 15),
    new LocalDate(2014, 3, 23));

DateInterval? overlappingInterval = firstInterval.Union(secondInterval);

Console.WriteLine(overlappingInterval);

Output:

[2014-03-07, 2014-03-23]

Exceptions
Type Condition
ArgumentException interval uses a different calendar to this date interval.

Operators

Equality(DateInterval, DateInterval)

Compares two DateInterval values for equality. See the type documentation for a description of equality semantics.
Since 2.0.x
Availability netstandard2.0
Declaration
public static bool operator ==(DateInterval lhs, DateInterval rhs)
Parameters
Type Name Description
DateInterval lhs The first value to compare
DateInterval rhs The second value to compare
Returns
Type Description
Boolean True if the two date intervals have the same properties; false otherwise.

Inequality(DateInterval, DateInterval)

Compares two DateInterval values for inequality. See the type documentation for a description of equality semantics.
Since 2.0.x
Availability netstandard2.0
Declaration
public static bool operator !=(DateInterval lhs, DateInterval rhs)
Parameters
Type Name Description
DateInterval lhs The first value to compare
DateInterval rhs The second value to compare
Returns
Type Description
Boolean False if the two date intervals have the same start and end date; true otherwise.

Explicit Interface Implementations

IEnumerable.GetEnumerator()

Since 2.3.x
Availability netstandard2.0
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type Description
IEnumerator

Implements

System.IEquatable<T>
System.Collections.Generic.IEnumerable<T>
System.Collections.IEnumerable
In this article
Back to top Generated by DocFX