W3cubDocs

/date-fns

areIntervalsOverlapping

Is the given time interval overlapping with another time interval?

Description

Is the given time interval overlapping with another time interval? Adjacent intervals do not count as overlapping.

Usage

// CommonJS
var areIntervalsOverlapping = require('date-fns/areIntervalsOverlapping')
// ES 2015
import areIntervalsOverlapping from 'date-fns/areIntervalsOverlapping'
// ESM
import { areIntervalsOverlapping } from 'date-fns'

Syntax

areIntervalsOverlapping(intervalLeft, intervalRight, [options])

Arguments

Name Description
intervalLeft

the first interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}

intervalRight

the second interval to compare. See [Interval]{@link https://date-fns.org/docs/Interval}

options

the object with options

options.inclusive

whether the comparison is inclusive or not

Returns

Description

whether the time intervals are overlapping

Exceptions

Type Description
TypeError

2 arguments required

RangeError

The start of an interval cannot be after its end

RangeError

Date in interval cannot be Invalid Date

Examples

// For overlapping time intervals:
areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
)
//=> true
// For non-overlapping time intervals:
areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
)
//=> false
// For adjacent time intervals:
areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) }
)
//=> false
// Using the inclusive option:
areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }
)
//=> false
areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
  { inclusive: true }
)
//=> true

© 2021 Sasha Koss and Lesha Koss
Licensed under the MIT License.
https://date-fns.org/v2.29.2/docs/areIntervalsOverlapping