differenceInDays.mjs 3.4 KB

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