Skip to content

Interface: Interval<IsValid>

An Interval object represents a half-open interval of time, where each endpoint is a DateTime. Conceptually, it is 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:

Type Parameters

Type ParameterDefault type
IsValid extends booleanDefaultValidity

Accessors

end

Get Signature

ts
get end(): IfValid<DateTime<true>, null, IsValid>;

Returns the end of the Interval. This is the first instant which is not part of the interval. (Interval is half-open).

Returns

IfValid<DateTime<true>, null, IsValid>


invalidExplanation

Get Signature

ts
get invalidExplanation(): IfValid<null, string | null, IsValid>;

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

Returns

IfValid<null, string | null, IsValid>


invalidReason

Get Signature

ts
get invalidReason(): IfValid<null, string, IsValid>;

Returns an error code if this Interval is invalid, or null if the Interval is valid

Returns

IfValid<null, string, IsValid>


isValid

Get Signature

ts
get isValid(): IfValid<true, false, IsValid>;

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

Returns

IfValid<true, false, IsValid>


lastDateTime

Get Signature

ts
get lastDateTime(): IfValid<DateTime<true>, null, IsValid>;

Returns the last DateTime included in the interval (since end is not part of the interval)

Returns

IfValid<DateTime<true>, null, IsValid>


start

Get Signature

ts
get start(): IfValid<DateTime<true>, null, IsValid>;

Returns the start of the Interval

Returns

IfValid<DateTime<true>, null, IsValid>

Methods

abutsEnd()

ts
abutsEnd(other: Interval): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterType
otherInterval

Returns

IfValid<boolean, false, IsValid>


abutsStart()

ts
abutsStart(other: Interval): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterType
otherInterval

Returns

IfValid<boolean, false, IsValid>


contains()

ts
contains(dateTime: DateTime): IfValid<boolean, false, IsValid>;

Return whether this Interval contains the specified DateTime.

Parameters

ParameterTypeDescription
dateTimeDateTime

Returns

IfValid<boolean, false, IsValid>


count()

ts
count(unit?: keyof DurationLikeObject, opts?: _UseLocaleWeekOption): IfValid<number, number, IsValid>;

Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. Unlike Interval#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?'

Parameters

ParameterTypeDescription
unit?keyof DurationLikeObjectthe unit of time to count. Defaults to 'milliseconds'.
opts?_UseLocaleWeekOption-

Returns

IfValid<number, number, IsValid>


difference()

ts
difference(...intervals: Interval<boolean>[]): (Interval<true> | Interval<false>)[];

Return Intervals representing the spans of time in this Interval that not overlap with any of the specified Intervals.

Parameters

ParameterType
...intervalsInterval<boolean>[]

Returns

(Interval<true> | Interval<false>)[]


divideEqually()

ts
divideEqually(numberOfParts: number): IfValid<Interval<boolean>[], [], IsValid>;

Split this Interval into the specified number of smaller intervals.

Parameters

ParameterTypeDescription
numberOfPartsnumberThe number of Intervals to divide the Interval into.

Returns

IfValid<Interval<boolean>[], [], IsValid>


engulfs()

ts
engulfs(other: Interval): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterType
otherInterval

Returns

IfValid<boolean, false, IsValid>


equals()

ts
equals(other: Interval): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterType
otherInterval

Returns

IfValid<boolean, false, IsValid>


hasSame()

ts
hasSame(unit: keyof DurationLikeObject): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterTypeDescription
unitkeyof DurationLikeObjectthe unit of time to check sameness on

Returns

IfValid<boolean, false, IsValid>


intersection()

ts
intersection(other: Interval): Interval<boolean> | null;

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 do not intersect.

Parameters

ParameterType
otherInterval

Returns

Interval<boolean> | null


isAfter()

ts
isAfter(dateTime: DateTime): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterTypeDescription
dateTimeDateTime

Returns

IfValid<boolean, false, IsValid>


isBefore()

ts
isBefore(dateTime: DateTime): IfValid<boolean, false, IsValid>;

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

Parameters

ParameterTypeDescription
dateTimeDateTime

Returns

IfValid<boolean, false, IsValid>


isEmpty()

ts
isEmpty(): boolean;

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

Returns

boolean


length()

ts
length(unit?: keyof DurationLikeObject): IfValid<number, number, IsValid>;

Returns the length of the Interval in the specified unit.

Parameters

ParameterTypeDescription
unit?keyof DurationLikeObjectthe unit (such as 'hours' or 'days') to return the length in.

Returns

IfValid<number, number, IsValid>


mapEndpoints()

ts
mapEndpoints(mapFn: (d: DateTime) => DateTime): Interval<true> | Interval<false>;

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

Parameters

ParameterType
mapFn(d: DateTime) => DateTime

Returns

Interval<true> | Interval<false>

Examples

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

overlaps()

ts
overlaps(other: Interval): boolean;

Return whether this Interval overlaps with the specified Interval

Parameters

ParameterType
otherInterval

Returns

boolean


set()

ts
set(values?: IntervalObject): Interval<true> | Interval<false>;

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

Parameters

ParameterTypeDescription
values?IntervalObjectthe values to set

Returns

Interval<true> | Interval<false>


splitAt()

ts
splitAt(...dateTimes: DateTime<boolean>[]): IfValid<Interval<boolean>[], [], IsValid>;

Split this Interval at each of the specified DateTimes

Parameters

ParameterTypeDescription
...dateTimesDateTime<boolean>[]the unit of time to count.

Returns

IfValid<Interval<boolean>[], [], IsValid>


splitBy()

ts
splitBy(duration: DurationLike): IfValid<Interval<boolean>[], [], IsValid>;

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

Parameters

ParameterTypeDescription
durationDurationLikeThe length of each resulting interval.

Returns

IfValid<Interval<boolean>[], [], IsValid>


toDuration()

ts
toDuration(unit?: keyof DurationLikeObject | keyof DurationLikeObject[], opts?: DiffOptions):
  | Duration<true>
| Duration<false>;

Return a Duration representing the time spanned by this interval.

Parameters

ParameterTypeDescription
unit?keyof DurationLikeObject | keyof DurationLikeObject[]the unit or units (such as 'hours' or 'days') to include in the duration. Defaults to ['milliseconds'].
opts?DiffOptionsoptions that affect the creation of the Duration

Returns

| Duration<true> | Duration<false>

Examples

ts
Interval.fromDateTimes(dt1, dt2).toDuration().toObject(); //=> { milliseconds: 88489257 }
ts
Interval.fromDateTimes(dt1, dt2).toDuration("days").toObject(); //=> { days: 1.0241812152777778 }
ts
Interval.fromDateTimes(dt1, dt2).toDuration(["hours", "minutes"]).toObject(); //=> { hours: 24, minutes: 34.82095 }
ts
Interval.fromDateTimes(dt1, dt2)
  .toDuration(["hours", "minutes", "seconds"])
  .toObject(); //=> { hours: 24, minutes: 34, seconds: 49.257 }
ts
Interval.fromDateTimes(dt1, dt2).toDuration("seconds").toObject(); //=> { seconds: 88489.257 }

toFormat()

ts
toFormat(dateFormat: string, opts?: {
  separator?: string;
}): IfValid<string, "Invalid Interval", IsValid>;

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

Parameters

ParameterTypeDescription
dateFormatstringthe format string. This string formats the start and end time. See DateTime.toFormat for details.
opts?{ separator?: string; }options
opts.separator?stringa separator to place between the start and end representations. Defaults to ' - '.

Returns

IfValid<string, "Invalid Interval", IsValid>


toISO()

ts
toISO(opts?: ToISOTimeOptions): IfValid<string, "Invalid Interval", IsValid>;

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

Parameters

ParameterTypeDescription
opts?ToISOTimeOptionsThe same options as DateTime#toISO

Returns

IfValid<string, "Invalid Interval", IsValid>

See

https://en.wikipedia.org/wiki/ISO_8601#Time_intervals


toISODate()

ts
toISODate(): IfValid<string, "Invalid Interval", IsValid>;

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

Returns

IfValid<string, "Invalid Interval", IsValid>

See

https://en.wikipedia.org/wiki/ISO_8601#Time_intervals


toISOTime()

ts
toISOTime(opts?: ToISOTimeOptions): IfValid<string, "Invalid Interval", IsValid>;

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

Parameters

ParameterTypeDescription
opts?ToISOTimeOptionsThe same options as DateTime.toISO

Returns

IfValid<string, "Invalid Interval", IsValid>

See

https://en.wikipedia.org/wiki/ISO_8601#Time_intervals


toLocaleString()

ts
toLocaleString(formatOpts?: DateTimeFormatOptions, opts?: LocaleOptions): IfValid<string, "Invalid Interval", IsValid>;

Returns a localized string representing this Interval. Accepts the same options as the Intl.DateTimeFormat constructor and any presets defined by Luxon, such as DateTime.DATE_FULL or DateTime.TIME_SIMPLE. The exact behavior of this method is browser-specific, but in general it will return an appropriate representation of the Interval in the assigned locale. Defaults to the system's locale if no locale has been specified.

Parameters

ParameterTypeDescription
formatOpts?DateTimeFormatOptionsEither a DateTime preset or Intl.DateTimeFormat constructor options. Defaults to DateTime.DATE_SHORT
opts?LocaleOptionsOptions to override the configuration of the start DateTime.

Returns

IfValid<string, "Invalid Interval", IsValid>

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

Examples

ts
Interval.fromISO("2022-11-07T09:00Z/2022-11-08T09:00Z").toLocaleString(); //=> 11/7/2022 – 11/8/2022
ts
Interval.fromISO("2022-11-07T09:00Z/2022-11-08T09:00Z").toLocaleString(
  DateTime.DATE_FULL,
); //=> November 7 – 8, 2022
ts
Interval.fromISO("2022-11-07T09:00Z/2022-11-08T09:00Z").toLocaleString(
  DateTime.DATE_FULL,
  { locale: "fr-FR" },
); //=> 7–8 novembre 2022
ts
Interval.fromISO("2022-11-07T17:00Z/2022-11-07T19:00Z").toLocaleString(
  DateTime.TIME_SIMPLE,
); //=> 6:00 – 8:00 PM
ts
Interval.fromISO("2022-11-07T17:00Z/2022-11-07T19:00Z").toLocaleString({
  weekday: "short",
  month: "short",
  day: "2-digit",
  hour: "2-digit",
  minute: "2-digit",
}); //=> Mon, Nov 07, 6:00 – 8:00 p

toString()

ts
toString(): IfValid<string, "Invalid Interval", IsValid>;

Returns a string representation of this Interval appropriate for debugging.

Returns

IfValid<string, "Invalid Interval", IsValid>


union()

ts
union(other: Interval): Interval<true> | Interval<false>;

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.

Parameters

ParameterType
otherInterval

Returns

Interval<true> | Interval<false>