UNPKG

1.94 kBTypeScriptView Raw
1/**
2 * @name differenceInDays
3 * @category Day Helpers
4 * @summary Get the number of full days between the given dates.
5 *
6 * @description
7 * Get the number of full day periods between two dates. Fractional days are
8 * truncated towards zero.
9 *
10 * One "full day" is the distance between a local time in one day to the same
11 * local time on the next or previous day. A full day can sometimes be less than
12 * or more than 24 hours if a daylight savings change happens between two dates.
13 *
14 * To ignore DST and only measure exact 24-hour periods, use this instead:
15 * `Math.trunc(differenceInHours(dateLeft, dateRight)/24)|0`.
16 *
17 * @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).
18 *
19 * @param dateLeft - The later date
20 * @param dateRight - The earlier date
21 *
22 * @returns The number of full days according to the local timezone
23 *
24 * @example
25 * // How many full days are between
26 * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
27 * const result = differenceInDays(
28 * new Date(2012, 6, 2, 0, 0),
29 * new Date(2011, 6, 2, 23, 0)
30 * )
31 * //=> 365
32 *
33 * @example
34 * // How many full days are between
35 * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
36 * const result = differenceInDays(
37 * new Date(2011, 6, 3, 0, 1),
38 * new Date(2011, 6, 2, 23, 59)
39 * )
40 * //=> 0
41 *
42 * @example
43 * // How many full days are between
44 * // 1 March 2020 0:00 and 1 June 2020 0:00 ?
45 * // Note: because local time is used, the
46 * // result will always be 92 days, even in
47 * // time zones where DST starts and the
48 * // period has only 92*24-1 hours.
49 * const result = differenceInDays(
50 * new Date(2020, 5, 1),
51 * new Date(2020, 2, 1)
52 * )
53 * //=> 92
54 */
55export declare function differenceInDays<DateType extends Date>(
56 dateLeft: DateType | number | string,
57 dateRight: DateType | number | string,
58): number;