differenceInDays.js 3.4 KB

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