Luxon Home Reference Source Repository
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 | object | Date, duration: Duration | number | object): Interval

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

public static

before(end: DateTime | object | Date, duration: Duration | number | object): 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(string: string, opts: object): Interval

Create an Interval from an ISO 8601 string

public static

invalid(reason: *): Interval

Create an invalid 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 whether this Interval's end is at least its start, i.e. 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 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

Returns a string representation of this Interval appropriate for the REPL.

public

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

public

isAfter(dateTime: DateTime): boolean

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

public

isBefore(dateTime: Datetime): boolean

Return 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

Return whether this Interval overlaps with the specified Interval

public

set(values: object): Interval

"Sets" the start and/or end dates.

public

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

Split this Interval at each of the specified DateTimes

public

splitBy(duration: Duration | number | object): [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 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 | object | Date, duration: Duration | number | object): Interval source

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

Params:

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

the length of the Interval.

Return:

Interval

public static before(end: DateTime | object | Date, duration: Duration | number | object): Interval source

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

Params:

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

the length of the Interval.

Return:

Interval

public static fromDateTimes(start: DateTime | object | Date, end: DateTime | object | Date): 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 | object | Date
end DateTime | object | Date

Return:

Interval

public static fromISO(string: string, opts: object): Interval source

Create an Interval from an ISO 8601 string

Params:

NameTypeAttributeDescription
string string

the ISO string to parse

opts object

options to pass {@see DateTime.fromISO}

Return:

Interval

public static invalid(reason: *): Interval source

Create an invalid Interval.

Params:

NameTypeAttributeDescription
reason *

Return:

Interval

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

Return:

DateTime

public get invalidReason: string: * source

Returns an explanation of why this Interval became invalid, or null if the Interval is valid

Return:

string

public get isValid: boolean: * source

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

Return:

boolean

public get start: DateTime: * source

Returns the start of the Interval

Return:

DateTime

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 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 inspect(): string source

Returns a string representation of this Interval appropriate for the REPL.

Return:

string

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.

Params:

NameTypeAttributeDescription
other Interval

Return:

Interval

public isAfter(dateTime: DateTime): boolean source

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

Params:

NameTypeAttributeDescription
dateTime DateTime

Return:

boolean

public isBefore(dateTime: Datetime): boolean source

Return 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 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: ...DateTimes): [Interval] source

Split this Interval at each of the specified DateTimes

Params:

NameTypeAttributeDescription
dateTimes ...DateTimes

the unit of time to count.

Return:

[Interval]

public splitBy(duration: Duration | number | object): [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 | number | object

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