Fancy date ranges for Moment.js with comprehensive range operations and iteration capabilities
npx @tessl/cli install tessl/npm-moment-range@4.0.0moment-range is a JavaScript/TypeScript library that extends Moment.js with comprehensive date range functionality. It provides a powerful DateRange class and extends Moment.js with range-specific methods, enabling creation, manipulation, querying, and iteration of date ranges with full timezone support.
npm install moment-rangemoment >= 2.0.0import Moment from 'moment';
import { extendMoment, DateRange } from 'moment-range';
const moment = extendMoment(Moment);For CommonJS:
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);Browser usage:
<script src="moment.js"></script>
<script src="moment-range.js"></script>
<script>
window['moment-range'].extendMoment(moment);
</script>import Moment from 'moment';
import { extendMoment } from 'moment-range';
const moment = extendMoment(Moment);
// Create a date range
const start = moment('2024-01-01');
const end = moment('2024-01-31');
const range = moment.range(start, end);
// Check if a date is within the range
const testDate = moment('2024-01-15');
console.log(testDate.within(range)); // true
// Iterate over range by day
for (const day of range.by('day')) {
console.log(day.format('YYYY-MM-DD'));
}
// Check if ranges overlap
const otherRange = moment.range('2024-01-15', '2024-02-15');
console.log(range.overlaps(otherRange)); // truemoment-range is built around several key components:
Create date ranges from various input formats including dates, moments, arrays, and ISO 8601 interval strings.
// Static creation methods
function moment.range(start: Date | Moment, end: Date | Moment): DateRange;
function moment.range(range: [Date | Moment, Date | Moment]): DateRange;
function moment.range(isoString: string): DateRange;
function moment.rangeFromInterval(interval: string, count?: number, date?: Moment): DateRange;
function moment.rangeFromISOString(isoString: string): DateRange;
// Constructor
class DateRange {
constructor(start: Date | Moment, end: Date | Moment);
constructor(range: [Date | Moment, Date | Moment]);
constructor(isoString: string);
}Query relationships between ranges and dates including containment, overlap, adjacency, and intersection operations.
// Range relationship methods
adjacent(other: DateRange): boolean;
contains(other: Date | Moment | DateRange, options?: ContainsOptions): boolean;
overlaps(other: DateRange, options?: OverlapOptions): boolean;
intersect(other: DateRange): DateRange | null;
isEqual(other: DateRange): boolean;
interface ContainsOptions {
excludeStart?: boolean;
excludeEnd?: boolean;
}
interface OverlapOptions {
adjacent?: boolean;
}Manipulate ranges through addition, subtraction, cloning, and boundary snapping operations.
// Range manipulation methods
add(other: DateRange, options?: { adjacent?: boolean }): DateRange | null;
subtract(other: DateRange): DateRange[];
clone(): DateRange;
snapTo(interval: string): DateRange;
center(): Moment;Iterate over ranges using various time intervals with customizable options for step size and boundary inclusion.
// Forward iteration
by(interval: string, options?: IterationOptions): Iterable<Moment>;
byRange(interval: DateRange, options?: IterationOptions): Iterable<Moment>;
// Reverse iteration
reverseBy(interval: string, options?: ReverseIterationOptions): Iterable<Moment>;
reverseByRange(interval: DateRange, options?: ReverseIterationOptions): Iterable<Moment>;
interface IterationOptions {
excludeEnd?: boolean;
step?: number;
}
interface ReverseIterationOptions {
excludeStart?: boolean;
step?: number;
}Utility methods and instance methods added to Moment.js for range-aware operations.
// Static utility methods
function moment.isRange(range: any): boolean;
// Instance methods
function moment.fn.within(range: DateRange): boolean;
function moment.fn.range(start?: Date | Moment, end?: Date | Moment): DateRange;class DateRange {
start: Moment;
end: Moment;
// Core methods
diff(unit?: string, precise?: boolean): number;
duration(unit?: string, precise?: boolean): number;
valueOf(): number;
toString(): string;
toDate(): [Date, Date];
}