UNPKG

3.51 kBJavaScriptView Raw
1import { normalizeDates } from "./_lib/normalizeDates.js";
2import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
3
4/**
5 * The {@link differenceInDays} function options.
6 */
7
8/**
9 * @name differenceInDays
10 * @category Day Helpers
11 * @summary Get the number of full days between the given dates.
12 *
13 * @description
14 * Get the number of full day periods between two dates. Fractional days are
15 * truncated towards zero.
16 *
17 * One "full day" is the distance between a local time in one day to the same
18 * local time on the next or previous day. A full day can sometimes be less than
19 * or more than 24 hours if a daylight savings change happens between two dates.
20 *
21 * To ignore DST and only measure exact 24-hour periods, use this instead:
22 * `Math.trunc(differenceInHours(dateLeft, dateRight)/24)|0`.
23 *
24 * @param laterDate - The later date
25 * @param earlierDate - The earlier date
26 * @param options - An object with options
27 *
28 * @returns The number of full days according to the local timezone
29 *
30 * @example
31 * // How many full days are between
32 * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
33 * const result = differenceInDays(
34 * new Date(2012, 6, 2, 0, 0),
35 * new Date(2011, 6, 2, 23, 0)
36 * )
37 * //=> 365
38 *
39 * @example
40 * // How many full days are between
41 * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
42 * const result = differenceInDays(
43 * new Date(2011, 6, 3, 0, 1),
44 * new Date(2011, 6, 2, 23, 59)
45 * )
46 * //=> 0
47 *
48 * @example
49 * // How many full days are between
50 * // 1 March 2020 0:00 and 1 June 2020 0:00 ?
51 * // Note: because local time is used, the
52 * // result will always be 92 days, even in
53 * // time zones where DST starts and the
54 * // period has only 92*24-1 hours.
55 * const result = differenceInDays(
56 * new Date(2020, 5, 1),
57 * new Date(2020, 2, 1)
58 * )
59 * //=> 92
60 */
61export function differenceInDays(laterDate, earlierDate, options) {
62 const [laterDate_, earlierDate_] = normalizeDates(
63 options?.in,
64 laterDate,
65 earlierDate,
66 );
67
68 const sign = compareLocalAsc(laterDate_, earlierDate_);
69 const difference = Math.abs(
70 differenceInCalendarDays(laterDate_, earlierDate_),
71 );
72
73 laterDate_.setDate(laterDate_.getDate() - sign * difference);
74
75 // Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
76 // If so, result must be decreased by 1 in absolute value
77 const isLastDayNotFull = Number(
78 compareLocalAsc(laterDate_, earlierDate_) === -sign,
79 );
80
81 const result = sign * (difference - isLastDayNotFull);
82 // Prevent negative zero
83 return result === 0 ? 0 : result;
84}
85
86// Like `compareAsc` but uses local time not UTC, which is needed
87// for accurate equality comparisons of UTC timestamps that end up
88// having the same representation in local time, e.g. one hour before
89// DST ends vs. the instant that DST ends.
90function compareLocalAsc(laterDate, earlierDate) {
91 const diff =
92 laterDate.getFullYear() - earlierDate.getFullYear() ||
93 laterDate.getMonth() - earlierDate.getMonth() ||
94 laterDate.getDate() - earlierDate.getDate() ||
95 laterDate.getHours() - earlierDate.getHours() ||
96 laterDate.getMinutes() - earlierDate.getMinutes() ||
97 laterDate.getSeconds() - earlierDate.getSeconds() ||
98 laterDate.getMilliseconds() - earlierDate.getMilliseconds();
99
100 if (diff < 0) return -1;
101 if (diff > 0) return 1;
102
103 // Return 0 if diff is 0; return NaN if diff is NaN
104 return diff;
105}
106
107// Fallback for modularized imports:
108export default differenceInDays;