DateRange

date~ DateRange

Represents a Range between two times

Constructor

new DateRange(startDate, endDate)

Parameters:
Name Type Description
startDate Date

the starting datetime of the range

endDate Date

the ending datetime of the range

Members

endDate :Date

The ending date

Type:
  • Date

startDate :Date

The starting date

Type:
  • Date

Methods

contains(dateToCheck) → {Boolean}

Determines if a datetime is within the range

Example
withinA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
withinB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
withinC = new Date(Date.UTC(2024, 11, 26, 14, 0, 0));
withinD = new Date(Date.UTC(2024, 11, 26, 15, 0, 0));

range = new utils.DateRange(withinB, withinD);
range.contains(withinA); // false - it was before the range

range.contains(withinB); // true
range.contains(withinC); // true
range.contains(withinD); // true
Parameters:
Name Type Description
dateToCheck Date

the value to test if it is within the date range

Returns:
  • if the value is within the range (true) or not (false)
Type
Boolean

duration() → {Number}

Determines the millisecond duration between the end and start time.

durationA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
durationB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
range = new utils.DateRange(durationA, durationB);

range.durationString(); // 1 hour in milliseconds; 1000 * 60 * 60;
Returns:
Type
Number

durationISO() → {String}

Determines the duration in days:hours:minutes:seconds.milliseconds

durationA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
durationB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
range = new utils.DateRange(durationA, durationB);

range.durationString(); // '0:01:00:00.0000';
Returns:
Type
String

durationString() → {String}

Determines the duration in a clear and understandable string;

durationA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
durationB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
range = new utils.DateRange(durationA, durationB);

range.durationString(); // '0 days, 1 hours, 0 minutes, 0.0 seconds';
Returns:
Type
String

isValid() → {Boolean}

Determines if both the startDate and endDate are valid dates.

Returns:
Type
Boolean

overlaps(targetDateRange) → {Boolean}

Whether this dateRange overlaps with a target dateRange.

Example
overlapA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
overlapB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
overlapC = new Date(Date.UTC(2024, 11, 26, 14, 0, 0));
overlapD = new Date(Date.UTC(2024, 11, 26, 15, 0, 0));

rangeBefore = new utils.DateRange(overlapA, overlapB);
rangeAfter = new utils.DateRange(overlapC, overlapD);

rangeBefore.overlaps(rangeAfter); // false
rangeAfter.overlaps(rangeBefore); // false

rangeBefore = new utils.DateRange(overlapA, overlapC);
rangeAfter = new utils.DateRange(overlapB, overlapD);

rangeBefore.overlaps(rangeAfter); // true
rangeAfter.overlaps(rangeBefore); // true
Parameters:
Name Type Description
targetDateRange DateRange

dateRange to compare

Returns:
Type
Boolean

reinitialize(startDate, endDate)

Reinitializes the object

(Sometimes useful for shifting times after the fact)

Parameters:
Name Type Description
startDate Date

the starting date

endDate Date

the ending date

toLocaleString() → {String}

Converts the daterange to a local string value

durationA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
durationB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
range = new utils.DateRange(durationA, durationB);

range.toLocaleString(); // '1/26/2025, 12:00:00 PM to 1/26/2025, 1:00:00 PM'
Returns:
Type
String

toString() → {String}

Converts the daterange to a string value

durationA = new Date(Date.UTC(2024, 11, 26, 12, 0, 0));
durationB = new Date(Date.UTC(2024, 11, 26, 13, 0, 0));
range = new utils.DateRange(durationA, durationB);

range.toString(); // '2025-01-26T12:00:00.000Z to 2025-01-26T13:00:00.000Z';
Returns:
Type
String

(static) startAndEndOfDay(targetDate) → {DateRange}

Creates a DateRange based on the start and end of the day UTC.

This is very useful for determining overlapping dates.

Parameters:
Name Type Description
targetDate Date

date to use to find the start and end UTC for

Returns:
Type
DateRange