Skip to content

isWithinRange()

isWithinRange(date, start, end): boolean

Defined in: utils/date/isWithinRange.ts:42

Check whether a given date falls within a closed date range [start, end] (inclusive on both ends).

The function:

  • Accepts Date, ISO-like string, or timestamp number for all inputs.
  • Converts them via toDateStrict.
  • Compares their underlying timestamps (milliseconds since epoch).
  • Is order-insensitive: if start is after end, it will swap them.

Parameters

date

The date to test.

string | number | Date

start

Start of the range (inclusive). Can be before or after end.

string | number | Date

end

End of the range (inclusive). Can be before or after start.

string | number | Date

Returns

boolean

true if date falls between start and end (including both), otherwise false.

Throws

When any of the inputs cannot be converted into a valid Date (as determined by toDateStrict).

Examples

ts
isWithinRange('2024-01-10', '2024-01-01', '2024-01-31') // true
isWithinRange('2024-02-01', '2024-01-01', '2024-01-31') // false
ts
// Order of start/end does not matter:
isWithinRange('2024-01-10', '2024-01-31', '2024-01-01') // true

Released under the MIT License.