index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Convert interval to duration
  16. *
  17. * @description
  18. * Convert a interval object to a duration object.
  19. *
  20. * @param {Interval} interval - the interval to convert to duration
  21. *
  22. * @returns {Duration} The duration Object
  23. * @throws {TypeError} Requires 2 arguments
  24. * @throws {RangeError} `start` must not be Invalid Date
  25. * @throws {RangeError} `end` must not be Invalid Date
  26. *
  27. * @example
  28. * // Get the duration between January 15, 1929 and April 4, 1968.
  29. * intervalToDuration({
  30. * start: new Date(1929, 0, 15, 12, 0, 0),
  31. * end: new Date(1968, 3, 4, 19, 5, 0)
  32. * })
  33. * // => { years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }
  34. */
  35. export default function intervalToDuration(_ref) {
  36. var start = _ref.start,
  37. end = _ref.end;
  38. requiredArgs(1, arguments);
  39. var dateLeft = toDate(start);
  40. var dateRight = toDate(end);
  41. if (!isValid(dateLeft)) {
  42. throw new RangeError('Start Date is invalid');
  43. }
  44. if (!isValid(dateRight)) {
  45. throw new RangeError('End Date is invalid');
  46. }
  47. var duration = {
  48. years: 0,
  49. months: 0,
  50. days: 0,
  51. hours: 0,
  52. minutes: 0,
  53. seconds: 0
  54. };
  55. var sign = compareAsc(dateLeft, dateRight);
  56. duration.years = Math.abs(differenceInYears(dateLeft, dateRight));
  57. var remainingMonths = sub(dateLeft, {
  58. years: sign * duration.years
  59. });
  60. duration.months = Math.abs(differenceInMonths(remainingMonths, dateRight));
  61. var remainingDays = sub(remainingMonths, {
  62. months: sign * duration.months
  63. });
  64. duration.days = Math.abs(differenceInDays(remainingDays, dateRight));
  65. var remainingHours = sub(remainingDays, {
  66. days: sign * duration.days
  67. });
  68. duration.hours = Math.abs(differenceInHours(remainingHours, dateRight));
  69. var remainingMinutes = sub(remainingHours, {
  70. hours: sign * duration.hours
  71. });
  72. duration.minutes = Math.abs(differenceInMinutes(remainingMinutes, dateRight));
  73. var remainingSeconds = sub(remainingMinutes, {
  74. minutes: sign * duration.minutes
  75. });
  76. duration.seconds = Math.abs(differenceInSeconds(remainingSeconds, dateRight));
  77. return duration;
  78. }