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:
- Creation To create an Interval, use fromDateTimes, after, before, or fromISO.
- Accessors Use start and end to get the start and end.
- Interrogation To analyze the Interval, use count, length, hasSame, contains, isAfter, or isBefore.
- Transformation To create other Intervals out of this one, use set, splitAt, splitBy, divideEqually, merge, xor, union, intersection, or difference.
- Comparison To compare this Interval to another one, use equals, overlaps, abutsStart, abutsEnd, engulfs.
- Output To convert the Interval into other representations, see toString, toISO, toISODate, toISOTime, toFormat, and toDuration.
Static Method Summary
Static Public Methods | ||
public static |
Create an Interval from a start DateTime and a Duration to extend to. |
|
public static |
Create an Interval from an end DateTime and a Duration to extend backwards to. |
|
public static |
Create an Interval from a start DateTime and an end DateTime. |
|
public static |
Create an Interval from an ISO 8601 string. |
|
public static |
Create an invalid Interval. |
|
public static |
isInterval(o: object): boolean Check if an object is an Interval. |
|
public static |
merge(intervals: [Interval]): [Interval] Merge an array of Intervals into a equivalent minimal set of Intervals. |
|
public static |
xor(intervals: [Interval]): [Interval] Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals. |
Member Summary
Public Members | ||
public get |
Returns the end of the Interval |
|
public get |
Returns an explanation of why this Interval became invalid, or null if the Interval is valid |
|
public get |
Returns an error code if this Interval is invalid, or null if the Interval is valid |
|
public get |
Returns whether this Interval's end is at least its start, meaning that the Interval isn't 'backwards'. |
|
public get |
Returns the start of the Interval |
Method Summary
Public Methods | ||
public |
Return whether this Interval's start is adjacent to the specified Interval's end. |
|
public |
abutsStart(other: Interval): boolean Return whether this Interval's end is adjacent to the specified Interval's start. |
|
public |
Return whether this Interval contains the specified DateTime. |
|
public |
Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. |
|
public |
difference(intervals: ...Interval): [Interval] Return an Interval representing the span of time in this Interval that doesn't overlap with any of the specified Intervals. |
|
public |
divideEqually(numberOfParts: number): [Interval] Split this Interval into the specified number of smaller intervals. |
|
public |
Return whether this Interval engulfs the start and end of the specified Interval. |
|
public |
Return whether this Interval has the same start and end as the specified Interval. |
|
public |
Returns whether this Interval's start and end are both in the same unit of time |
|
public |
intersection(other: Interval): Interval Return an Interval representing the intersection of this Interval and the specified Interval. |
|
public |
Return whether this Interval's start is after the specified DateTime. |
|
public |
Return whether this Interval's end is before the specified DateTime. |
|
public |
Return whether this Interval has the same start and end DateTimes. |
|
public |
Returns the length of the Interval in the specified unit. |
|
public |
mapEndpoints(mapFn: function): Interval Run mapFn on the interval start and end, returning a new Interval from the resulting DateTimes |
|
public |
Return whether this Interval overlaps with the specified Interval |
|
public |
"Sets" the start and/or end dates. |
|
public |
splitAt(dateTimes: ...[DateTime]): [Interval] Split this Interval at each of the specified DateTimes |
|
public |
Split this Interval into smaller Intervals, each of the specified length. |
|
public |
toDuration(unit: string | string[], opts: Object): Duration Return a Duration representing the time spanned by this interval. |
|
public |
Returns a string representation of this Interval formatted according to the specified format string. |
|
public |
Returns an ISO 8601-compliant string representation of this Interval. |
|
public |
Returns an ISO 8601-compliant string representation of date of this Interval. |
|
public |
Returns an ISO 8601-compliant string representation of time of this Interval. |
|
public |
Returns a string representation of this Interval appropriate for debugging. |
|
public |
Return an Interval representing the union of this Interval and the specified Interval. |
Static Public Methods
public static after(start: DateTime | Date | Object, duration: Duration | Object | number): Interval source
Create an Interval from a start DateTime and a Duration to extend to.
public static before(end: DateTime | Date | Object, duration: Duration | Object | number): Interval source
Create an Interval from an end DateTime and a Duration to extend backwards to.
public static fromDateTimes(start: DateTime | Date | Object, end: DateTime | Date | Object): Interval source
Create an Interval from a start DateTime and an end DateTime. Inclusive of the start but not the end.
public static fromISO(text: string, opts: Object): Interval source
Create an Interval from an ISO 8601 string.
Accepts <start>/<end>
, <start>/<duration>
, and <duration>/<end>
formats.
Params:
Name | Type | Attribute | Description |
text | string | the ISO string to parse |
|
opts | Object |
|
options to pass DateTime.fromISO and optionally Duration.fromISO |
public static invalid(reason: string, explanation: string): Interval source
Create an invalid Interval.
public static isInterval(o: object): boolean source
Check if an object is an Interval. Works across context boundaries
Params:
Name | Type | Attribute | Description |
o | object |
public static merge(intervals: [Interval]): [Interval] source
Merge an array of Intervals into a equivalent minimal set of Intervals. Combines overlapping and adjacent Intervals.
Params:
Name | Type | Attribute | Description |
intervals | [Interval] |
Return:
[Interval] |
public static xor(intervals: [Interval]): [Interval] source
Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals.
Params:
Name | Type | Attribute | Description |
intervals | [Interval] |
Return:
[Interval] |
Public Members
public get invalidExplanation: string source
Returns an explanation of why this Interval became invalid, or null if the Interval is valid
public get invalidReason: string source
Returns an error code if this Interval is invalid, or null if the Interval is valid
Public Methods
public abutsEnd(other: Interval): boolean source
Return whether this Interval's start is adjacent to the specified Interval's end.
Params:
Name | Type | Attribute | Description |
other | Interval |
public abutsStart(other: Interval): boolean source
Return whether this Interval's end is adjacent to the specified Interval's start.
Params:
Name | Type | Attribute | Description |
other | Interval |
public contains(dateTime: DateTime): boolean source
Return whether this Interval contains the specified DateTime.
Params:
Name | Type | Attribute | Description |
dateTime | DateTime |
public count(unit: string): number source
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?'
Params:
Name | Type | Attribute | Description |
unit | string |
|
the unit of time to count. |
public difference(intervals: ...Interval): [Interval] source
Return an Interval representing the span of time in this Interval that doesn't overlap with any of the specified Intervals.
Params:
Name | Type | Attribute | Description |
intervals | ...Interval |
Return:
[Interval] |
public divideEqually(numberOfParts: number): [Interval] source
Split this Interval into the specified number of smaller intervals.
Params:
Name | Type | Attribute | Description |
numberOfParts | number | The number of Intervals to divide the Interval into. |
Return:
[Interval] |
public engulfs(other: Interval): boolean source
Return whether this Interval engulfs the start and end of the specified Interval.
Params:
Name | Type | Attribute | Description |
other | Interval |
public equals(other: Interval): boolean source
Return whether this Interval has the same start and end as the specified Interval.
Params:
Name | Type | Attribute | Description |
other | Interval |
public hasSame(unit: string): boolean source
Returns whether this Interval's start and end are both in the same unit of time
Params:
Name | Type | Attribute | Description |
unit | string | the unit of time to check sameness on |
public intersection(other: Interval): Interval source
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.
Params:
Name | Type | Attribute | Description |
other | Interval |
public isAfter(dateTime: DateTime): boolean source
Return whether this Interval's start is after the specified DateTime.
Params:
Name | Type | Attribute | Description |
dateTime | DateTime |
public isBefore(dateTime: DateTime): boolean source
Return whether this Interval's end is before the specified DateTime.
Params:
Name | Type | Attribute | Description |
dateTime | DateTime |
public length(unit: string): number source
Returns the length of the Interval in the specified unit.
Params:
Name | Type | Attribute | Description |
unit | string | the unit (such as 'hours' or 'days') to return the length in. |
public mapEndpoints(mapFn: function): Interval source
Run mapFn on the interval start and end, returning a new Interval from the resulting DateTimes
Params:
Name | Type | Attribute | Description |
mapFn | function |
Example:
Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.toUTC())
Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.plus({ hours: 2 }))
public overlaps(other: Interval): boolean source
Return whether this Interval overlaps with the specified Interval
Params:
Name | Type | Attribute | Description |
other | Interval |
public set(values: Object): Interval source
"Sets" the start and/or end dates. Returns a newly-constructed Interval.
public splitAt(dateTimes: ...[DateTime]): [Interval] source
Split this Interval at each of the specified DateTimes
Params:
Name | Type | Attribute | Description |
dateTimes | ...[DateTime] | the unit of time to count. |
Return:
[Interval] |
public splitBy(duration: Duration | Object | number): [Interval] source
Split this Interval into smaller Intervals, each of the specified length. Left over time is grouped into a smaller interval
Return:
[Interval] |
public toDuration(unit: string | string[], opts: Object): Duration source
Return a Duration representing the time spanned by this interval.
Params:
Name | Type | Attribute | Description |
unit | string | string[] |
|
the unit or units (such as 'hours' or 'days') to include in the duration. |
opts | Object | options that affect the creation of the Duration |
|
opts.conversionAccuracy | string |
|
the conversion system to use |
Example:
Interval.fromDateTimes(dt1, dt2).toDuration().toObject() //=> { milliseconds: 88489257 }
Interval.fromDateTimes(dt1, dt2).toDuration('days').toObject() //=> { days: 1.0241812152777778 }
Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes']).toObject() //=> { hours: 24, minutes: 34.82095 }
Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes', 'seconds']).toObject() //=> { hours: 24, minutes: 34, seconds: 49.257 }
Interval.fromDateTimes(dt1, dt2).toDuration('seconds').toObject() //=> { seconds: 88489.257 }
public toFormat(dateFormat: string, opts: Object): string source
Returns a string representation of this Interval formatted according to the specified format string.
Params:
Name | Type | Attribute | Description |
dateFormat | string | the format string. This string formats the start and end time. See DateTime.toFormat for details. |
|
opts | Object | options |
|
opts.separator | string |
|
a separator to place between the start and end representations |
public toISO(opts: Object): string source
Returns an ISO 8601-compliant string representation of this Interval.
Params:
Name | Type | Attribute | Description |
opts | Object | The same options as DateTime.toISO |
public toISODate(): string source
Returns an ISO 8601-compliant string representation of date of this Interval. The time components are ignored.
public toISOTime(opts: Object): string source
Returns an ISO 8601-compliant string representation of time of this Interval. The date components are ignored.
Params:
Name | Type | Attribute | Description |
opts | Object | The same options as DateTime.toISO |
public toString(): string source
Returns a string representation of this Interval appropriate for debugging.