Class Interval

An Interval object represents a half-open interval of time, where each endpoint is a DateTime. Conceptually, it's a container for those two endpoints, accompanied by methods for creating, parsing, interrogating, comparing, transforming, and formatting them.

Here is a brief overview of the most commonly used methods and getters in Interval:

Hierarchy

  • Interval

Accessors

  • get invalidReason(): null | string
  • Returns an error code if this Interval is invalid, or null if the Interval is valid

    Returns null | string

  • get isValid(): boolean
  • Returns whether this Interval's end is at least its start, meaning that the Interval isn't 'backwards'.

    Returns boolean

Methods

  • Return whether this Interval's start is adjacent to the specified Interval's end.

    Returns

    Parameters

    Returns boolean

  • Return whether this Interval's end is adjacent to the specified Interval's start.

    Returns

    Parameters

    Returns boolean

  • Return whether this Interval contains the specified DateTime.

    Returns

    Parameters

    Returns boolean

  • Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. Unlike length this counts sections of the calendar, not periods of time, e.g. specifying 'day' asks 'what dates are included in this interval?', not 'how many days long is this interval?'

    Returns

    Parameters

    • Optional unit: keyof DurationObject = "milliseconds"

      the unit of time to count.

    Returns number

  • Returns Intervals representing the span(s) of time in this Interval that don't overlap with any of the specified Intervals.

    Returns

    Parameters

    Returns Interval[]

  • Split this Interval into the specified number of smaller intervals.

    Returns

    Parameters

    • numberOfParts: number

      The number of Intervals to divide the Interval into.

    Returns Interval[]

  • Return whether this Interval engulfs the start and end of the specified Interval.

    Returns

    Parameters

    Returns boolean

  • Return whether this Interval has the same start and end as the specified Interval.

    Returns

    Parameters

    Returns boolean

  • Returns whether this Interval's start and end are both in the same unit of time

    Returns

    Parameters

    Returns boolean

  • Return an Interval representing the intersection of this Interval and the specified Interval. Specifically, the resulting Interval has the maximum start time and the minimum end time of the two Intervals. Returns null if the intersection is empty, meaning, the intervals don't intersect.

    Returns

    Parameters

    Returns null | Interval

  • Return whether this Interval's start is after the specified DateTime.

    Returns

    Parameters

    Returns boolean

  • Return whether this Interval's end is before the specified DateTime.

    Returns

    Parameters

    Returns boolean

  • Return whether this Interval has the same start and end DateTimes.

    Returns

    Returns boolean

  • Returns the length of the Interval in the specified unit.

    Returns

    Parameters

    • Optional unit: keyof DurationObject = "milliseconds"

      the unit (such as 'hours' or 'days') to return the length in.

    Returns number

  • Run mapFn on the interval start and end, returning a new Interval from the resulting DateTimes

    Returns

    Example

    Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.toUTC())
    

    Example

    Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.plus({ hours: 2 }))
    

    Parameters

    Returns Interval

  • Return whether this Interval overlaps with the specified Interval

    Returns

    Parameters

    Returns boolean

  • Split this Interval at each of the specified DateTimes

    Returns

    Parameters

    • Rest ...dateTimes: DateTimeLike[]

      the unit of time to count.

    Returns Interval[]

  • Split this Interval into smaller Intervals, each of the specified length. Left over time is grouped into a smaller interval

    Returns

    Parameters

    • duration: DurationLike

      The length of each resulting interval, as a Duration object.

    Returns Interval[]

  • Return a Duration representing the time spanned by this interval.

    Example

    Interval.fromDateTimes(dt1, dt2).toDuration().toObject() //=> { milliseconds: 88489257 }
    

    Example

    Interval.fromDateTimes(dt1, dt2).toDuration('days').toObject() //=> { days: 1.0241812152777778 }
    

    Example

    Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes']).toObject() //=> { hours: 24, minutes: 34.82095 }
    

    Example

    Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes', 'seconds']).toObject() //=> { hours: 24, minutes: 34, seconds: 49.257 }
    

    Example

    Interval.fromDateTimes(dt1, dt2).toDuration('seconds').toObject() //=> { seconds: 88489.257 }
    

    Returns

    Parameters

    • Optional unit: keyof DurationObject | (keyof DurationObject)[] = "milliseconds"

      the unit or units (such as 'hours' or 'days') to include in the duration.

    • opts: DurationOptions = {}

      options that affect the creation of the Duration

    Returns Duration

  • Returns a string representation of this Interval formatted according to the specified format string.

    Returns

    Parameters

    • dateFormat: string

      the format string. This string formats the start and end time. See toFormat for details.

    • options: {
          separator: string;
      } = ...

      options

      • separator: string

    Returns string

  • Returns a string representation of this Interval appropriate for debugging.

    Returns

    Returns string

  • Return an Interval representing the union of this Interval and the specified Interval. Specifically, the resulting Interval has the minimum start time and the maximum end time of the two Intervals.

    Returns

    Parameters

    Returns Interval

  • Create an Interval from a start DateTime and a Duration to extend to.

    Returns

    Parameters

    • start: DateTimeLike
    • duration: DurationLike

      the length of the Interval, as a Duration object.

    Returns Interval

  • Create an Interval from an end DateTime and a Duration to extend backwards to.

    Returns

    Parameters

    • end: DateTimeLike
    • duration: DurationLike

      the length of the Interval, as a Duration object.

    Returns Interval

  • Create an Interval from a start DateTime and an end DateTime. Inclusive of the start but not the end.

    Returns

    Parameters

    • start: DateTimeLike
    • end: DateTimeLike

    Returns Interval

  • Create an invalid Interval.

    Returns

    Parameters

    • reason: string | Invalid

      simple string of why this Interval is invalid. Should not contain parameters or anything else data-dependent

    • Optional explanation: string

      longer explanation, may include parameters and other useful debugging information

    Returns Interval

  • Check if an object is an Interval. Works across context boundaries

    Returns

    Parameters

    • o: unknown

    Returns o is Interval

  • Merge an array of Intervals into a equivalent minimal set of Intervals. Combines overlapping and adjacent Intervals.

    Returns

    Parameters

    Returns Interval[]

  • Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals.

    Returns

    Parameters

    Returns Interval[]

Generated using TypeDoc