Immutable date wrapper library for JavaScript with comprehensive date/time manipulation, timezone support, and internationalization capabilities.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The Interval class represents time ranges between two DateTimes. It provides methods for interval relationships, set operations, splitting intervals, and converting to other representations.
Methods for creating Interval instances.
/**
* Create an invalid Interval
* @param reason - Reason for invalidity
* @param explanation - Additional explanation
*/
static invalid(reason: string, explanation?: string): Interval;
/**
* Create Interval from start and end DateTimes
* @param start - Start DateTime
* @param end - End DateTime
*/
static fromDateTimes(start: DateTime, end: DateTime): Interval;
/**
* Create Interval from start DateTime plus Duration
* @param start - Start DateTime
* @param duration - Duration to add to start
*/
static after(start: DateTime, duration: Duration): Interval;
/**
* Create Interval from end DateTime minus Duration
* @param end - End DateTime
* @param duration - Duration to subtract from end
*/
static before(end: DateTime, duration: Duration): Interval;
/**
* Parse Interval from ISO 8601 interval string
* @param text - ISO 8601 interval string (e.g., "2023-01-01/2023-01-31")
* @param opts - Options including setZone
*/
static fromISO(text: string, opts?): Interval;
/**
* Check if object is an Interval instance
* @param o - Object to check
*/
static isInterval(o: any): boolean;Methods for working with arrays of intervals.
/**
* Merge overlapping intervals in array
* @param intervals - Array of intervals to merge
*/
static merge(intervals: Interval[]): Interval[];
/**
* Find symmetric difference of intervals
* @param intervals - Array of intervals
*/
static xor(intervals: Interval[]): Interval[];Access interval endpoints and validation.
// Endpoints
start: DateTime;
end: DateTime;
// Validation
isValid: boolean;
invalidReason: string | null;
invalidExplanation: string | null;/**
* Length of interval as Duration
* @param unit - Unit for the length (optional)
*/
length(unit?: string): number;
/**
* Count of calendar periods in interval
* @param unit - Calendar unit (years, months, days, etc.)
*/
count(unit: string): number;
/**
* Check if start and end are in same time unit
* @param unit - Unit to check (year, month, day, etc.)
*/
hasSame(unit: string): boolean;
/**
* Check if interval is empty (start equals end)
*/
isEmpty(): boolean;/**
* Check if interval is after specified DateTime
* @param dateTime - DateTime to compare against
*/
isAfter(dateTime: DateTime): boolean;
/**
* Check if interval is before specified DateTime
* @param dateTime - DateTime to compare against
*/
isBefore(dateTime: DateTime): boolean;
/**
* Check if interval contains specified DateTime
* @param dateTime - DateTime to check
*/
contains(dateTime: DateTime): boolean;/**
* Set start and/or end DateTime
* @param values - Object with start and/or end properties
*/
set(values: {start?: DateTime, end?: DateTime}): Interval;/**
* Split interval at specified DateTimes
* @param dateTimes - DateTimes to split at
*/
splitAt(...dateTimes: DateTime[]): Interval[];
/**
* Split interval by duration increments
* @param duration - Duration for each split
*/
splitBy(duration: Duration): Interval[];
/**
* Divide interval into equal parts
* @param numberOfParts - Number of equal parts
*/
divideEqually(numberOfParts: number): Interval[];/**
* Check if this interval overlaps with another
* @param other - Other interval to check
*/
overlaps(other: Interval): boolean;
/**
* Check if this interval's end abuts other's start
* @param other - Other interval to check
*/
abutsStart(other: Interval): boolean;
/**
* Check if this interval's start abuts other's end
* @param other - Other interval to check
*/
abutsEnd(other: Interval): boolean;
/**
* Check if this interval completely contains another
* @param other - Other interval to check
*/
engulfs(other: Interval): boolean;
/**
* Check equality with other Interval
* @param other - Interval to compare
*/
equals(other: Interval): boolean;/**
* Return intersection with another interval
* @param other - Other interval
*/
intersection(other: Interval): Interval | null;
/**
* Return union with another interval (if they overlap or abut)
* @param other - Other interval
*/
union(other: Interval): Interval | null;
/**
* Return intervals representing difference from other intervals
* @param intervals - Intervals to subtract from this one
*/
difference(...intervals: Interval[]): Interval[];/**
* String representation
*/
toString(): string;
/**
* ISO 8601 interval string
* @param opts - Options for formatting DateTimes
*/
toISO(opts?): string;
/**
* ISO 8601 date interval string (dates only)
*/
toISODate(): string;
/**
* ISO 8601 time interval string (times only)
* @param opts - Options for time formatting
*/
toISOTime(opts?): string;
/**
* Format interval with custom format
* @param dateFormat - Format for start and end DateTimes
* @param opts - Options for formatting
*/
toFormat(dateFormat: string, opts?): string;/**
* Convert interval to Duration
* @param unit - Unit for the duration (optional)
* @param opts - Options including conversionAccuracy
*/
toDuration(unit?: string, opts?): Duration;
/**
* Transform endpoints with function
* @param mapFn - Function to transform each endpoint
*/
mapEndpoints(mapFn: (dt: DateTime) => DateTime): Interval;import { DateTime, Duration, Interval } from "luxon";
// Creating intervals
const start = DateTime.local(2023, 10, 1);
const end = DateTime.local(2023, 10, 31);
const interval = Interval.fromDateTimes(start, end);
// Alternative creation methods
const afterStart = Interval.after(start, Duration.fromObject({ days: 30 }));
const beforeEnd = Interval.before(end, Duration.fromObject({ days: 30 }));
const fromISO = Interval.fromISO("2023-10-01/2023-10-31");
// Basic properties
console.log(interval.length('days')); // 30
console.log(interval.count('weeks')); // 4
console.log(interval.isEmpty()); // false
// DateTime relationships
const midMonth = DateTime.local(2023, 10, 15);
console.log(interval.contains(midMonth)); // true
console.log(interval.isAfter(DateTime.local(2023, 9, 30))); // true
// Splitting intervals
const weeklyIntervals = interval.splitBy(Duration.fromObject({ weeks: 1 }));
const quarters = interval.divideEqually(4);
// Interval relationships
const overlap = Interval.fromDateTimes(
DateTime.local(2023, 10, 15),
DateTime.local(2023, 11, 15)
);
console.log(interval.overlaps(overlap)); // true
// Set operations
const intersection = interval.intersection(overlap);
const union = interval.union(overlap);
// Formatting
console.log(interval.toISO()); // "2023-10-01T00:00:00.000-07:00/2023-10-31T00:00:00.000-07:00"
console.log(interval.toFormat("yyyy-MM-dd")); // "2023-10-01 – 2023-10-31"
// Working with arrays of intervals
const intervals = [
Interval.fromDateTimes(DateTime.local(2023, 1, 1), DateTime.local(2023, 1, 15)),
Interval.fromDateTimes(DateTime.local(2023, 1, 10), DateTime.local(2023, 1, 20)),
Interval.fromDateTimes(DateTime.local(2023, 1, 25), DateTime.local(2023, 1, 31))
];
const merged = Interval.merge(intervals); // Merges overlapping intervalsInterval A: |-------|
Interval B: |-------| (overlaps)
Interval C: |-------| (abuts)
Interval D: |---| (engulfed by A)
Interval E: |------------------| (engulfs A)Luxon supports these ISO 8601 interval formats:
start/end: 2023-01-01T00:00:00Z/2023-01-31T23:59:59Zstart/duration: 2023-01-01T00:00:00Z/P1Mduration/end: P1M/2023-01-31T23:59:59Z