differenceInDays.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
  3. /**
  4. * The {@link differenceInDays} function options.
  5. */
  6. /**
  7. * @name differenceInDays
  8. * @category Day Helpers
  9. * @summary Get the number of full days between the given dates.
  10. *
  11. * @description
  12. * Get the number of full day periods between two dates. Fractional days are
  13. * truncated towards zero.
  14. *
  15. * One "full day" is the distance between a local time in one day to the same
  16. * local time on the next or previous day. A full day can sometimes be less than
  17. * or more than 24 hours if a daylight savings change happens between two dates.
  18. *
  19. * To ignore DST and only measure exact 24-hour periods, use this instead:
  20. * `Math.trunc(differenceInHours(dateLeft, dateRight)/24)|0`.
  21. *
  22. * @param laterDate - The later date
  23. * @param earlierDate - The earlier date
  24. * @param options - An object with options
  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. export function differenceInDays(laterDate, earlierDate, options) {
  60. const [laterDate_, earlierDate_] = normalizeDates(
  61. options?.in,
  62. laterDate,
  63. earlierDate,
  64. );
  65. const sign = compareLocalAsc(laterDate_, earlierDate_);
  66. const difference = Math.abs(
  67. differenceInCalendarDays(laterDate_, earlierDate_),
  68. );
  69. laterDate_.setDate(laterDate_.getDate() - sign * difference);
  70. // Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
  71. // If so, result must be decreased by 1 in absolute value
  72. const isLastDayNotFull = Number(
  73. compareLocalAsc(laterDate_, earlierDate_) === -sign,
  74. );
  75. const result = sign * (difference - isLastDayNotFull);
  76. // Prevent negative zero
  77. return result === 0 ? 0 : result;
  78. }
  79. // Like `compareAsc` but uses local time not UTC, which is needed
  80. // for accurate equality comparisons of UTC timestamps that end up
  81. // having the same representation in local time, e.g. one hour before
  82. // DST ends vs. the instant that DST ends.
  83. function compareLocalAsc(laterDate, earlierDate) {
  84. const diff =
  85. laterDate.getFullYear() - earlierDate.getFullYear() ||
  86. laterDate.getMonth() - earlierDate.getMonth() ||
  87. laterDate.getDate() - earlierDate.getDate() ||
  88. laterDate.getHours() - earlierDate.getHours() ||
  89. laterDate.getMinutes() - earlierDate.getMinutes() ||
  90. laterDate.getSeconds() - earlierDate.getSeconds() ||
  91. laterDate.getMilliseconds() - earlierDate.getMilliseconds();
  92. if (diff < 0) return -1;
  93. if (diff > 0) return 1;
  94. // Return 0 if diff is 0; return NaN if diff is NaN
  95. return diff;
  96. }
  97. // Fallback for modularized imports:
  98. export default differenceInDays;