Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

79 строки
2.6 KiB

  1. import compareAsc from '../compareAsc/index.js';
  2. import differenceInYears from '../differenceInYears/index.js';
  3. import differenceInMonths from '../differenceInMonths/index.js';
  4. import differenceInDays from '../differenceInDays/index.js';
  5. import differenceInHours from '../differenceInHours/index.js';
  6. import differenceInMinutes from '../differenceInMinutes/index.js';
  7. import differenceInSeconds from '../differenceInSeconds/index.js';
  8. import isValid from '../isValid/index.js';
  9. import requiredArgs from '../_lib/requiredArgs/index.js';
  10. import toDate from '../toDate/index.js';
  11. import sub from '../sub/index.js';
  12. /**
  13. * @name intervalToDuration
  14. * @category Common Helpers
  15. * @summary Get the Duration between 2 dates in an Interval Object
  16. *
  17. * @param {Interval} interval The Interval Object
  18. *
  19. * @returns {Duration} The Duration Object
  20. * @throws {TypeError} Requires 2 Arguments
  21. * @throws {RangeError} `start` must not be Invalid Date
  22. * @throws {RangeError} `end` must not be Invalid Date
  23. *
  24. * @example
  25. * // Get the Duration between January 15, 1929 and April 4, 1968.
  26. * const result = intervalToDuration({
  27. * new Date(1929, 0, 15, 12, 0, 0),
  28. * new Date(1968, 3, 4, 19, 5, 0)
  29. * })
  30. * // => { years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }
  31. */
  32. export default function intervalToDuration(_ref) {
  33. var start = _ref.start,
  34. end = _ref.end;
  35. requiredArgs(1, arguments);
  36. var dateLeft = toDate(start);
  37. var dateRight = toDate(end);
  38. if (!isValid(dateLeft)) {
  39. throw new RangeError('Start Date is invalid');
  40. }
  41. if (!isValid(dateRight)) {
  42. throw new RangeError('End Date is invalid');
  43. }
  44. var duration = {
  45. years: 0,
  46. months: 0,
  47. days: 0,
  48. hours: 0,
  49. minutes: 0,
  50. seconds: 0
  51. };
  52. var sign = compareAsc(dateLeft, dateRight);
  53. duration.years = Math.abs(differenceInYears(dateLeft, dateRight));
  54. var remainingMonths = sub(dateLeft, {
  55. years: sign * duration.years
  56. });
  57. duration.months = Math.abs(differenceInMonths(remainingMonths, dateRight));
  58. var remainingDays = sub(remainingMonths, {
  59. months: sign * duration.months
  60. });
  61. duration.days = Math.abs(differenceInDays(remainingDays, dateRight));
  62. var remainingHours = sub(remainingDays, {
  63. days: sign * duration.days
  64. });
  65. duration.hours = Math.abs(differenceInHours(remainingHours, dateRight));
  66. var remainingMinutes = sub(remainingHours, {
  67. hours: sign * duration.hours
  68. });
  69. duration.minutes = Math.abs(differenceInMinutes(remainingMinutes, dateRight));
  70. var remainingSeconds = sub(remainingMinutes, {
  71. minutes: sign * duration.minutes
  72. });
  73. duration.seconds = Math.abs(differenceInSeconds(remainingSeconds, dateRight));
  74. return duration;
  75. }