Represents a date range with start and end dates.

DateRange is immutable and ensures that the end date is not before the start date. Provides utility methods for checking overlaps, containment, and duration.

const result = DateRange.create(
new Date('2024-01-01'),
new Date('2024-12-31')
);
if (result.isSuccess) {
const range = result.value;
console.log(range.durationInDays()); // 365
}

Hierarchy (View Summary)

Methods

  • Compares this Value Object with another for equality. Value Objects are equal if they are of the same type and all equality components match.

    Parameters

    • other: ValueObject

      The other Value Object to compare with

    Returns boolean

    true if the Value Objects are equal, false otherwise

  • Creates a DateRange instance with validation.

    Parameters

    • startDate: Date

      The start date of the range

    • endDate: Date

      The end date of the range

    Returns Result<DateRange, DomainError>

    Success with DateRange or Failure with DomainError

    const result = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-12-31')
    );
    if (result.isSuccess) {
    console.log(result.value.startDate); // 2024-01-01
    }

    const invalid = DateRange.create(
    new Date('2024-12-31'),
    new Date('2024-01-01')
    );
    if (invalid.isFailure) {
    console.log(invalid.error.code); // "INVALID_DATE_RANGE"
    }
  • Creates a DateRange from ISO date strings.

    Parameters

    • startDateStr: string

      The start date as ISO string

    • endDateStr: string

      The end date as ISO string

    Returns Result<DateRange, DomainError>

    Success with DateRange or Failure with DomainError

    const result = DateRange.fromStrings('2024-01-01', '2024-12-31');
    
  • Calculates the duration of the range in milliseconds.

    Returns number

    The duration in milliseconds

    const range = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-01-02')
    ).unwrap();
    console.log(range.durationInMs()); // 86400000
  • Calculates the duration of the range in days.

    Returns number

    The duration in days

    const range = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-01-08')
    ).unwrap();
    console.log(range.durationInDays()); // 7
  • Checks if a date is within this range (inclusive).

    Parameters

    • date: Date

      The date to check

    Returns boolean

    true if the date is within the range

    const range = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-12-31')
    ).unwrap();
    console.log(range.contains(new Date('2024-06-15'))); // true
    console.log(range.contains(new Date('2025-01-01'))); // false
  • Checks if this range overlaps with another range.

    Parameters

    • other: DateRange

      The other DateRange to check

    Returns boolean

    true if the ranges overlap

    const range1 = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-06-30')
    ).unwrap();
    const range2 = DateRange.create(
    new Date('2024-06-01'),
    new Date('2024-12-31')
    ).unwrap();
    console.log(range1.overlaps(range2)); // true
  • Checks if this range completely contains another range.

    Parameters

    • other: DateRange

      The other DateRange to check

    Returns boolean

    true if this range contains the other range

    const range1 = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-12-31')
    ).unwrap();
    const range2 = DateRange.create(
    new Date('2024-06-01'),
    new Date('2024-06-30')
    ).unwrap();
    console.log(range1.containsRange(range2)); // true
  • Checks if this range is adjacent to another range (touching but not overlapping).

    Parameters

    • other: DateRange

      The other DateRange to check

    Returns boolean

    true if the ranges are adjacent

    const range1 = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-06-30')
    ).unwrap();
    const range2 = DateRange.create(
    new Date('2024-07-01'),
    new Date('2024-12-31')
    ).unwrap();
    console.log(range1.isAdjacentTo(range2)); // true
  • Creates a new DateRange that is the intersection of this range and another.

    Parameters

    Returns Result<DateRange, DomainError>

    Success with intersection DateRange or Failure if no overlap

    const range1 = DateRange.create(
    new Date('2024-01-01'),
    new Date('2024-06-30')
    ).unwrap();
    const range2 = DateRange.create(
    new Date('2024-06-01'),
    new Date('2024-12-31')
    ).unwrap();
    const intersection = range1.intersection(range2);
    // intersection.value covers 2024-06-01 to 2024-06-30

Properties

startDate: Date
endDate: Date