diff options
| author | Pinapelz <yukais@pinapelz.com> | 2025-06-28 17:26:46 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2025-06-28 17:43:56 -0700 |
| commit | e4fa1e69e7ebfb627c7198fd1a9881e9327ec4d4 (patch) | |
| tree | 06284a538a6008eca75051399e47db4e5d50301c /node_modules/date-fns/esm/isWithinInterval/index.js | |
initial commit: scaffolding
Diffstat (limited to 'node_modules/date-fns/esm/isWithinInterval/index.js')
| -rw-r--r-- | node_modules/date-fns/esm/isWithinInterval/index.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/node_modules/date-fns/esm/isWithinInterval/index.js b/node_modules/date-fns/esm/isWithinInterval/index.js new file mode 100644 index 0000000..f8fd012 --- /dev/null +++ b/node_modules/date-fns/esm/isWithinInterval/index.js @@ -0,0 +1,53 @@ +import toDate from "../toDate/index.js"; +import requiredArgs from "../_lib/requiredArgs/index.js"; +/** + * @name isWithinInterval + * @category Interval Helpers + * @summary Is the given date within the interval? + * + * @description + * Is the given date within the interval? (Including start and end.) + * + * @param {Date|Number} date - the date to check + * @param {Interval} interval - the interval to check + * @returns {Boolean} the date is within the interval + * @throws {TypeError} 2 arguments required + * @throws {RangeError} The start of an interval cannot be after its end + * @throws {RangeError} Date in interval cannot be `Invalid Date` + * + * @example + * // For the date within the interval: + * isWithinInterval(new Date(2014, 0, 3), { + * start: new Date(2014, 0, 1), + * end: new Date(2014, 0, 7) + * }) + * //=> true + * + * @example + * // For the date outside of the interval: + * isWithinInterval(new Date(2014, 0, 10), { + * start: new Date(2014, 0, 1), + * end: new Date(2014, 0, 7) + * }) + * //=> false + * + * @example + * // For date equal to interval start: + * isWithinInterval(date, { start, end: date }) // => true + * + * @example + * // For date equal to interval end: + * isWithinInterval(date, { start: date, end }) // => true + */ +export default function isWithinInterval(dirtyDate, interval) { + requiredArgs(2, arguments); + var time = toDate(dirtyDate).getTime(); + var startTime = toDate(interval.start).getTime(); + var endTime = toDate(interval.end).getTime(); + + // Throw an exception if start date is after end date or if any date is `Invalid Date` + if (!(startTime <= endTime)) { + throw new RangeError('Invalid interval'); + } + return time >= startTime && time <= endTime; +}
\ No newline at end of file |
