index.js 3.3 KB

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