index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInCalendarDays;
  6. var _index = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfDay/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var MILLISECONDS_IN_DAY = 86400000;
  11. /**
  12. * @name differenceInCalendarDays
  13. * @category Day Helpers
  14. * @summary Get the number of calendar days between the given dates.
  15. *
  16. * @description
  17. * Get the number of calendar days between the given dates. This means that the times are removed
  18. * from the dates and then the difference in days is calculated.
  19. *
  20. * ### v2.0.0 breaking changes:
  21. *
  22. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  23. *
  24. * @param {Date|Number} dateLeft - the later date
  25. * @param {Date|Number} dateRight - the earlier date
  26. * @returns {Number} the number of calendar days
  27. * @throws {TypeError} 2 arguments required
  28. *
  29. * @example
  30. * // How many calendar days are between
  31. * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
  32. * const result = differenceInCalendarDays(
  33. * new Date(2012, 6, 2, 0, 0),
  34. * new Date(2011, 6, 2, 23, 0)
  35. * )
  36. * //=> 366
  37. * // How many calendar days are between
  38. * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
  39. * const result = differenceInCalendarDays(
  40. * new Date(2011, 6, 3, 0, 1),
  41. * new Date(2011, 6, 2, 23, 59)
  42. * )
  43. * //=> 1
  44. */
  45. function differenceInCalendarDays(dirtyDateLeft, dirtyDateRight) {
  46. (0, _index3.default)(2, arguments);
  47. var startOfDayLeft = (0, _index2.default)(dirtyDateLeft);
  48. var startOfDayRight = (0, _index2.default)(dirtyDateRight);
  49. var timestampLeft = startOfDayLeft.getTime() - (0, _index.default)(startOfDayLeft);
  50. var timestampRight = startOfDayRight.getTime() - (0, _index.default)(startOfDayRight); // Round the number of days to the nearest integer
  51. // because the number of milliseconds in a day is not constant
  52. // (e.g. it's different in the day of the daylight saving time clock shift)
  53. return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_DAY);
  54. }
  55. module.exports = exports.default;