Luxon Interval and Duration Arithmetic
Model calendar-aware time spans with Luxon Interval and Duration, splitting ranges into months, comparing overlaps, and humanizing output.
Code
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:
luxonis 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
Date Add/Subtract CalculatorAdd or subtract days, weeks, months, or years from a date, clamping month ends such as January 31 plus one month.Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.
FAQ
- Does this require external packages?
- Yes — `luxon`. Add it to your project before running the snippet.
Report a problem with this exampleOpens a pre-filled issue on GitHub.