Luxon Interval and Duration Arithmetic

Model calendar-aware time spans with Luxon Interval and Duration, splitting ranges into months, comparing overlaps, and humanizing output.

JavascriptCli Tooling

Code

javascriptsnippet
23 lines
import { DateTime, Interval, Duration } from 'luxon';

const start = DateTime.fromISO('2026-01-15', { zone: 'utc' });
const end = DateTime.fromISO('2026-10-22', { zone: 'utc' });

const span = Interval.fromDateTimes(start, end);
console.log('months covered:', span.count('months'));
console.log('is before now:', span.isBefore(DateTime.utc()));

const sprint = Interval.fromDateTimes(
  DateTime.fromISO('2026-10-01'),
  DateTime.fromISO('2026-10-15'),
);
console.log('overlaps sprint:', span.overlaps(sprint));

const elapsed = Duration.fromMillis(span.length('milliseconds'));
console.log('quarters:', Math.floor(elapsed.as('quarters')));

const budget = Duration.fromObject({ hours: 3, minutes: 45 });
console.log('budget in minutes:', budget.as('minutes'));

const iso = span.toISO();
console.log('iso interval:', iso);

What the code does

From luxon it pulls DateTime, Interval, Duration — the imports name exactly which library surface the snippet touches.

The file’s state is a handful of top-level bindings: start, end, span, sprint.

What to watch for

  • Plan for an install step: luxon is a real dependency, resolved by the package manager.

Applying it

Running the file prints labeled results such as months covered:, is before now:, overlaps sprint: — each operation’s effect is visible in the output.

Statements that matter

const elapsed = Duration.fromMillis(span.length('milliseconds'));
const start = DateTime.fromISO('2026-01-15', { zone: 'utc' });
const budget = Duration.fromObject({ hours: 3, minutes: 45 });

Interactive Tools for This Topic

FAQ

Does this require external packages?
Yes — `luxon`. Add it to your project before running the snippet.