differenceInDays.cjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. exports.differenceInDays = differenceInDays;
  3. var _index = require("./_lib/normalizeDates.cjs");
  4. var _index2 = require("./differenceInCalendarDays.cjs");
  5. /**
  6. * The {@link differenceInDays} function options.
  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. */
  61. function differenceInDays(laterDate, earlierDate, options) {
  62. const [laterDate_, earlierDate_] = (0, _index.normalizeDates)(
  63. options?.in,
  64. laterDate,
  65. earlierDate,
  66. );
  67. const sign = compareLocalAsc(laterDate_, earlierDate_);
  68. const difference = Math.abs(
  69. (0, _index2.differenceInCalendarDays)(laterDate_, earlierDate_),
  70. );
  71. laterDate_.setDate(laterDate_.getDate() - sign * difference);
  72. // Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
  73. // If so, result must be decreased by 1 in absolute value
  74. const isLastDayNotFull = Number(
  75. compareLocalAsc(laterDate_, earlierDate_) === -sign,
  76. );
  77. const result = sign * (difference - isLastDayNotFull);
  78. // Prevent negative zero
  79. return result === 0 ? 0 : result;
  80. }
  81. // Like `compareAsc` but uses local time not UTC, which is needed
  82. // for accurate equality comparisons of UTC timestamps that end up
  83. // having the same representation in local time, e.g. one hour before
  84. // DST ends vs. the instant that DST ends.
  85. function compareLocalAsc(laterDate, earlierDate) {
  86. const diff =
  87. laterDate.getFullYear() - earlierDate.getFullYear() ||
  88. laterDate.getMonth() - earlierDate.getMonth() ||
  89. laterDate.getDate() - earlierDate.getDate() ||
  90. laterDate.getHours() - earlierDate.getHours() ||
  91. laterDate.getMinutes() - earlierDate.getMinutes() ||
  92. laterDate.getSeconds() - earlierDate.getSeconds() ||
  93. laterDate.getMilliseconds() - earlierDate.getMilliseconds();
  94. if (diff < 0) return -1;
  95. if (diff > 0) return 1;
  96. // Return 0 if diff is 0; return NaN if diff is NaN
  97. return diff;
  98. }