index.js 3.5 KB

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