Blame view

天文台pc/tianwentai-ui/node_modules/date-fns/isWithinInterval.js 1.46 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  "use strict";
  exports.isWithinInterval = isWithinInterval;
  var _index = require("./toDate.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.)
   *
   * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
   *
   * @param date - The date to check
   * @param interval - The interval to check
   *
   * @returns The date is within the interval
   *
   * @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
   */
  function isWithinInterval(date, interval) {
    const time = +(0, _index.toDate)(date);
    const [startTime, endTime] = [
      +(0, _index.toDate)(interval.start),
      +(0, _index.toDate)(interval.end),
    ].sort((a, b) => a - b);
  
    return time >= startTime && time <= endTime;
  }