index.js 3.6 KB

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