differenceInCalendarDays.cjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. exports.differenceInCalendarDays = differenceInCalendarDays;
  3. var _index = require("./_lib/getTimezoneOffsetInMilliseconds.cjs");
  4. var _index2 = require("./_lib/normalizeDates.cjs");
  5. var _index3 = require("./constants.cjs");
  6. var _index4 = require("./startOfDay.cjs");
  7. /**
  8. * The {@link differenceInCalendarDays} function options.
  9. */
  10. /**
  11. * @name differenceInCalendarDays
  12. * @category Day Helpers
  13. * @summary Get the number of calendar days between the given dates.
  14. *
  15. * @description
  16. * Get the number of calendar days between the given dates. This means that the times are removed
  17. * from the dates and then the difference in days is calculated.
  18. *
  19. * @param laterDate - The later date
  20. * @param earlierDate - The earlier date
  21. * @param options - The options object
  22. *
  23. * @returns The number of calendar days
  24. *
  25. * @example
  26. * // How many calendar days are between
  27. * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
  28. * const result = differenceInCalendarDays(
  29. * new Date(2012, 6, 2, 0, 0),
  30. * new Date(2011, 6, 2, 23, 0)
  31. * )
  32. * //=> 366
  33. * // How many calendar days are between
  34. * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
  35. * const result = differenceInCalendarDays(
  36. * new Date(2011, 6, 3, 0, 1),
  37. * new Date(2011, 6, 2, 23, 59)
  38. * )
  39. * //=> 1
  40. */
  41. function differenceInCalendarDays(laterDate, earlierDate, options) {
  42. const [laterDate_, earlierDate_] = (0, _index2.normalizeDates)(
  43. options?.in,
  44. laterDate,
  45. earlierDate,
  46. );
  47. const laterStartOfDay = (0, _index4.startOfDay)(laterDate_);
  48. const earlierStartOfDay = (0, _index4.startOfDay)(earlierDate_);
  49. const laterTimestamp =
  50. +laterStartOfDay -
  51. (0, _index.getTimezoneOffsetInMilliseconds)(laterStartOfDay);
  52. const earlierTimestamp =
  53. +earlierStartOfDay -
  54. (0, _index.getTimezoneOffsetInMilliseconds)(earlierStartOfDay);
  55. // Round the number of days to the nearest integer because the number of
  56. // milliseconds in a day is not constant (e.g. it's different in the week of
  57. // the daylight saving time clock shift).
  58. return Math.round(
  59. (laterTimestamp - earlierTimestamp) / _index3.millisecondsInDay,
  60. );
  61. }