or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmoment-extensions.mdrange-creation.mdrange-iteration.mdrange-manipulation.mdrange-querying.md
tile.json

tessl/npm-moment-range

Fancy date ranges for Moment.js with comprehensive range operations and iteration capabilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/moment-range@4.0.x

To install, run

npx @tessl/cli install tessl/npm-moment-range@4.0.0

index.mddocs/

moment-range

moment-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.

Package Information

  • Package Name: moment-range
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install moment-range
  • Peer Dependencies: moment >= 2.0.0

Core Imports

import 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>

Basic Usage

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)); // true

Architecture

moment-range is built around several key components:

  • DateRange Class: Core class providing range operations, iteration, and manipulation methods
  • Moment Extensions: Static methods added to moment for range creation and utility functions
  • Instance Extensions: Methods added to moment instances for range-aware operations
  • Iterator Protocol: ES6 Symbol.iterator support for efficient range iteration
  • Timezone Support: Full timezone awareness through moment.parseZone integration

Capabilities

Date Range Creation

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);
}

Range Creation

Range Querying

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;
}

Range Querying

Range Manipulation

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;

Range Manipulation

Range Iteration

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;
}

Range Iteration

Moment Extensions

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;

Moment Extensions

Types

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];
}