Luxon Manual Reference
import Interval from 'luxon/src/interval.js'
public class | source

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:

Static Method Summary

Static Public Methods
public static

after(start: DateTime | Date | Object, duration: Duration | Object | number): Interval

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

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

fromISO(text: string, opts: Object): Interval

Create an Interval from an ISO 8601 string.

public static

invalid(reason: string, explanation: string): Interval

Create an invalid Interval.

public static

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

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

public

contains(dateTime: DateTime): boolean

Return whether this Interval contains the specified DateTime.

public

count(unit: string): number

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

Return an Interval representing the intersection of this Interval and the specified Interval.

public

isAfter(dateTime: DateTime): boolean

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

public

isBefore(dateTime: DateTime): boolean

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

public

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

public

length(unit: string): number

Returns the length of the Interval in the specified unit.

public

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

set(values: Object): Interval

"Sets" the start and/or end dates.

public

splitAt(dateTimes: ...[DateTime]): [Interval]

Split this Interval at each of the specified DateTimes

public

splitBy(duration: Duration | Object | number): [Interval]

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

toFormat(dateFormat: string, opts: Object): string

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

public

toISO(opts: Object): string

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.

Params:

NameTypeAttributeDescription
start DateTime | Date | Object
duration Duration | Object | number

the length of the Interval.

Return:

Interval

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.

Params:

NameTypeAttributeDescription
end DateTime | Date | Object
duration Duration | Object | number

the length of the Interval.

Return:

Interval

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.

Params:

NameTypeAttributeDescription
start DateTime | Date | Object
end DateTime | Date | Object

Return:

Interval

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:

NameTypeAttributeDescription
text string

the ISO string to parse

opts Object
  • optional

options to pass DateTime.fromISO and optionally Duration.fromISO

Return:

Interval

See:

public static invalid(reason: string, explanation: string): Interval source

Create an invalid Interval.

Params:

NameTypeAttributeDescription
reason string

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

explanation string
  • optional
  • default: null

longer explanation, may include parameters and other useful debugging information

Return:

Interval

public static isInterval(o: object): boolean source

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

Params:

NameTypeAttributeDescription
o object

Return:

boolean

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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
intervals [Interval]

Return:

[Interval]

Public Members

public get end: DateTime source

Returns the end of the Interval

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 get isValid: boolean source

Returns whether this Interval's end is at least its start, meaning that the Interval isn't 'backwards'.

public get start: DateTime source

Returns the start of the Interval

Public Methods

public abutsEnd(other: Interval): boolean source

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

Params:

NameTypeAttributeDescription
other Interval

Return:

boolean

public abutsStart(other: Interval): boolean source

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

Params:

NameTypeAttributeDescription
other Interval

Return:

boolean

public contains(dateTime: DateTime): boolean source

Return whether this Interval contains the specified DateTime.

Params:

NameTypeAttributeDescription
dateTime DateTime

Return:

boolean

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:

NameTypeAttributeDescription
unit string
  • optional
  • default: 'milliseconds'

the unit of time to count.

Return:

number

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:

NameTypeAttributeDescription
intervals ...Interval

Return:

[Interval]

public divideEqually(numberOfParts: number): [Interval] source

Split this Interval into the specified number of smaller intervals.

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
other Interval

Return:

boolean

public equals(other: Interval): boolean source

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

Params:

NameTypeAttributeDescription
other Interval

Return:

boolean

public hasSame(unit: string): boolean source

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

Params:

NameTypeAttributeDescription
unit string

the unit of time to check sameness on

Return:

boolean

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:

NameTypeAttributeDescription
other Interval

Return:

Interval

public isAfter(dateTime: DateTime): boolean source

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

Params:

NameTypeAttributeDescription
dateTime DateTime

Return:

boolean

public isBefore(dateTime: DateTime): boolean source

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

Params:

NameTypeAttributeDescription
dateTime DateTime

Return:

boolean

public isEmpty(): boolean source

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

Return:

boolean

public length(unit: string): number source

Returns the length of the Interval in the specified unit.

Params:

NameTypeAttributeDescription
unit string

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

Return:

number

public mapEndpoints(mapFn: function): Interval source

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

Params:

NameTypeAttributeDescription
mapFn function

Return:

Interval

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:

NameTypeAttributeDescription
other Interval

Return:

boolean

public set(values: Object): Interval source

"Sets" the start and/or end dates. Returns a newly-constructed Interval.

Params:

NameTypeAttributeDescription
values Object

the values to set

values.start DateTime

the starting DateTime

values.end DateTime

the ending DateTime

Return:

Interval

public splitAt(dateTimes: ...[DateTime]): [Interval] source

Split this Interval at each of the specified DateTimes

Params:

NameTypeAttributeDescription
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

Params:

NameTypeAttributeDescription
duration Duration | Object | number

The length of each resulting interval.

Return:

[Interval]

public toDuration(unit: string | string[], opts: Object): Duration source

Return a Duration representing the time spanned by this interval.

Params:

NameTypeAttributeDescription
unit string | string[]
  • optional
  • default: ['milliseconds']

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
  • optional
  • default: 'casual'

the conversion system to use

Return:

Duration

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:

NameTypeAttributeDescription
dateFormat string

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

opts Object

options

opts.separator string
  • optional
  • default: ' – '

a separator to place between the start and end representations

Return:

string

public toISO(opts: Object): string source

Returns an ISO 8601-compliant string representation of this Interval.

Params:

NameTypeAttributeDescription
opts Object

The same options as DateTime.toISO

Return:

string

See:

public toISODate(): string source

Returns an ISO 8601-compliant string representation of date of this Interval. The time components are ignored.

Return:

string

See:

public toISOTime(opts: Object): string source

Returns an ISO 8601-compliant string representation of time of this Interval. The date components are ignored.

Params:

NameTypeAttributeDescription
opts Object

The same options as DateTime.toISO

Return:

string

See:

public toString(): string source

Returns a string representation of this Interval appropriate for debugging.

Return:

string

public union(other: Interval): Interval source

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.

Params:

NameTypeAttributeDescription
other Interval

Return:

Interval